Skip to content

Commit

Permalink
Add digital signature func
Browse files Browse the repository at this point in the history
  • Loading branch information
huy committed Apr 27, 2021
1 parent 2702fb2 commit c0832d8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# digital-signature-python
# Digital Signature Python

Argorithm: hmac-sha256

Run: ```python3 ./main/main.py```

Version: python3.x
10 changes: 10 additions & 0 deletions main/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from util.digital_signature import DigitalSignature

if __name__ == '__main__':
signature1 = DigitalSignature(data='abc',
secret='6a87c625-e186-43bc-8160-b6e80e1a910b').get_signature()

signature2 = DigitalSignature(data='abc',
secret='6a87c625-e186-43bc-8160-b6e80e1a910b').get_signature()

print('{}\n{}'.format(signature1, signature2))
28 changes: 28 additions & 0 deletions main/util/digital_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import hashlib
import hmac


class DigitalSignature:
__data: bytes
__secret: bytes
__encode: str

def __init__(self,
data: str,
secret: str,
encode='utf-8'):
self.__encode = encode
self.__data = bytes(data, self.__encode)
self.__secret = bytes(secret, self.__encode)

def get_signature(self):
signature = self.__calculate_hmac(secret=self.__secret,
data=self.__data)

return signature.hexdigest()

@staticmethod
def __calculate_hmac(secret: bytes,
data: bytes,
algorithm=hashlib.sha256):
return hmac.new(secret, data, algorithm)
Empty file added requirements.txt
Empty file.

0 comments on commit c0832d8

Please sign in to comment.