basicswap_miserver/basicswap/util/crypto.py

42 lines
909 B
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2024-04-07 19:27:46 +00:00
# Copyright (c) 2022-2024 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
2024-04-07 19:27:46 +00:00
from basicswap.contrib.blake256.blake256 import blake_hash
2024-04-22 18:59:22 +00:00
from Crypto.Hash import HMAC, RIPEMD160, SHA256, SHA512 # pycryptodome
2024-04-07 19:27:46 +00:00
def sha256(data: bytes) -> bytes:
h = SHA256.new()
h.update(data)
return h.digest()
2024-04-22 18:59:22 +00:00
def sha512(data: bytes) -> bytes:
h = SHA512.new()
h.update(data)
return h.digest()
2024-04-07 19:27:46 +00:00
def ripemd160(data: bytes) -> bytes:
h = RIPEMD160.new()
h.update(data)
return h.digest()
2024-04-07 19:27:46 +00:00
def blake256(data: bytes) -> bytes:
return blake_hash(data)
2024-04-22 18:59:22 +00:00
def hash160(data: bytes) -> bytes:
return ripemd160(sha256(data))
def hmac_sha512(secret: bytes, data: bytes) -> bytes:
h = HMAC.new(secret, digestmod=SHA512)
h.update(data)
return h.digest()