commit c3ab13d4f37ba1fc59f2259eef948ca8e5fb620f
parent 6faa5d7491ae264720012fef7cc8c54f46c674d4
Author: lash <dev@holbrook.no>
Date: Wed, 29 Mar 2023 10:23:30 +0100
Expose keccak string to bytes method
Diffstat:
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,3 +1,5 @@
+- 0.4.13
+ * Expose string to bytes keccak hash
- 0.4.12
* Exit 1 when no modules found for chainlib-gen
- 0.4.11
diff --git a/chainlib/hash.py b/chainlib/hash.py
@@ -3,6 +3,21 @@ import sha3
from hexathon import strip_0x
+def keccak256(s):
+ """Binary representation of Keccak256 hash of utf-8 string content.
+
+ :param s: utf-8 string to hash
+ :type s: str
+ :rtype: str
+ :returns: Hex-value of keccak256 hash
+ """
+ h = sha3.keccak_256()
+ if (type(s).__name__ == 'str'):
+ s = s.encode('utf-8')
+ h.update(s)
+ return h.digest()
+
+
def keccak256_hex(s):
"""Hex representation of Keccak256 hash of utf-8 string content.
@@ -11,9 +26,7 @@ def keccak256_hex(s):
:rtype: str
:returns: Hex-value of keccak256 hash
"""
- h = sha3.keccak_256()
- h.update(s.encode('utf-8'))
- return h.digest().hex()
+ return keccak256(s).hex()
def keccak256_string_to_hex(s):
diff --git a/setup.cfg b/setup.cfg
@@ -3,7 +3,7 @@ name=chainlib
license=AGPLv3+
author_email=dev@holbrook.no
description=Generic blockchain access library and tooling
-version=0.4.12
+version=0.4.13
url=https://git.defalsify.org/chainlib
author=Louis Holbrook