2019-07-20 00:01:16 +00:00
|
|
|
import base64, binascii
|
2019-09-09 18:26:11 +00:00
|
|
|
|
|
|
|
import unpaddedbase32
|
2019-07-20 00:01:16 +00:00
|
|
|
import nacl.encoding, nacl.signing, nacl.exceptions
|
2019-09-09 18:26:11 +00:00
|
|
|
|
|
|
|
from onionrutils import bytesconverter
|
2019-09-10 06:05:59 +00:00
|
|
|
from onionrutils import mnemonickeys
|
2019-07-20 00:01:16 +00:00
|
|
|
import logger
|
|
|
|
def ed_sign(data, key, encodeResult=False):
|
|
|
|
'''Ed25519 sign data'''
|
2019-09-09 18:26:11 +00:00
|
|
|
key = unpaddedbase32.repad(bytesconverter.str_to_bytes(key))
|
2019-07-20 00:01:16 +00:00
|
|
|
try:
|
|
|
|
data = data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
key = nacl.signing.SigningKey(seed=key, encoder=nacl.encoding.Base32Encoder)
|
|
|
|
retData = ''
|
|
|
|
if encodeResult:
|
|
|
|
retData = key.sign(data, encoder=nacl.encoding.Base64Encoder).signature.decode() # .encode() is not the same as nacl.encoding
|
|
|
|
else:
|
|
|
|
retData = key.sign(data).signature
|
|
|
|
return retData
|
|
|
|
|
|
|
|
def ed_verify(data, key, sig, encodedData=True):
|
|
|
|
'''Verify signed data (combined in nacl) to an ed25519 key'''
|
2019-09-09 18:26:11 +00:00
|
|
|
key = unpaddedbase32.repad(bytesconverter.str_to_bytes(key))
|
2019-07-20 00:01:16 +00:00
|
|
|
try:
|
|
|
|
key = nacl.signing.VerifyKey(key=key, encoder=nacl.encoding.Base32Encoder)
|
|
|
|
except nacl.exceptions.ValueError:
|
|
|
|
return False
|
|
|
|
except binascii.Error:
|
|
|
|
logger.warn('Could not load key for verification, invalid padding')
|
|
|
|
return False
|
|
|
|
retData = False
|
|
|
|
sig = base64.b64decode(sig)
|
|
|
|
try:
|
|
|
|
data = data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2019-10-08 02:32:33 +00:00
|
|
|
try:
|
|
|
|
retData = key.verify(data, sig) # .encode() is not the same as nacl.encoding
|
|
|
|
except nacl.exceptions.BadSignatureError:
|
|
|
|
pass
|
2019-07-20 00:01:16 +00:00
|
|
|
return retData
|