document python crypto apis
This commit is contained in:
parent
7fb1d7247c
commit
d268cf3b57
27
docs/api.md
27
docs/api.md
|
@ -9,16 +9,28 @@ Syntax used below is a valid Python function declaration with type hints defined
|
||||||
``` python
|
``` python
|
||||||
def trezor.crypto.base58.encode(data: bytes) -> str
|
def trezor.crypto.base58.encode(data: bytes) -> str
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Convert bytes to base58 encoded string.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
def trezor.crypto.base58.decode(string: str) -> bytes
|
def trezor.crypto.base58.decode(string: str) -> bytes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Convert base58 encoded string to bytes.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
def trezor.crypto.base58.encode_check(data: bytes) -> str
|
def trezor.crypto.base58.encode_check(data: bytes) -> str
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Convert bytes to base58 encoded string, append checksum.
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
def trezor.crypto.base58.decode_check(string: str) -> bytes
|
def trezor.crypto.base58.decode_check(string: str) -> bytes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Convert base58 encoded string to bytes and verify checksum.
|
||||||
|
|
||||||
|
|
||||||
###trezor.crypto.curve
|
###trezor.crypto.curve
|
||||||
|
|
||||||
####trezor.crypto.curve.ed25519
|
####trezor.crypto.curve.ed25519
|
||||||
|
@ -200,6 +212,21 @@ Returns the digest of hashed data.
|
||||||
def trezor.crypto.hmac.new(key, msg, digestmod) -> Hmac
|
def trezor.crypto.hmac.new(key, msg, digestmod) -> Hmac
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Creates a HMAC context object.
|
||||||
|
|
||||||
|
``` python
|
||||||
|
def Hmac.update(self, msg: bytes) -> None
|
||||||
|
```
|
||||||
|
|
||||||
|
Update the context with data.
|
||||||
|
|
||||||
|
``` python
|
||||||
|
def Hmac.digest(self) -> bytes
|
||||||
|
```
|
||||||
|
|
||||||
|
Returns the digest of processed data.
|
||||||
|
|
||||||
|
|
||||||
##trezor.msg
|
##trezor.msg
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
|
|
16
docs/api.py
16
docs/api.py
|
@ -1,10 +1,12 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
def process_file(fn):
|
def process_file(fn):
|
||||||
mod, ext = os.path.splitext(fn)
|
mod, ext = os.path.splitext(fn)
|
||||||
src = open('../%s' % (fn)).readlines()
|
src = open('../%s' % (fn)).readlines()
|
||||||
r = []
|
r = []
|
||||||
|
cls = ''
|
||||||
if ext in ['.h', '.c']:
|
if ext in ['.h', '.c']:
|
||||||
for l in src:
|
for l in src:
|
||||||
l = l.rstrip()
|
l = l.rstrip()
|
||||||
|
@ -24,6 +26,20 @@ def process_file(fn):
|
||||||
r.append('``` python')
|
r.append('``` python')
|
||||||
r.append('def %s.' % mod + l[4:-1])
|
r.append('def %s.' % mod + l[4:-1])
|
||||||
r.append('```')
|
r.append('```')
|
||||||
|
elif l.startswith('### '):
|
||||||
|
r.append(l[4:])
|
||||||
|
elif l.startswith('###'):
|
||||||
|
r.append('')
|
||||||
|
elif l.startswith('class '):
|
||||||
|
cls = re.match('class ([A-Za-z0-9_]*)', l).group(1)
|
||||||
|
elif l.startswith(' def ') and not l.startswith(' def __init__'):
|
||||||
|
r.append('``` python')
|
||||||
|
r.append('def %s.' % cls + l[8:-1])
|
||||||
|
r.append('```')
|
||||||
|
elif l.startswith(' ### '):
|
||||||
|
r.append(l[8:])
|
||||||
|
elif l.startswith(' ###'):
|
||||||
|
r.append('')
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -19,6 +19,9 @@ from .hashlib import sha256
|
||||||
_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||||
|
|
||||||
def encode(data: bytes) -> str:
|
def encode(data: bytes) -> str:
|
||||||
|
###
|
||||||
|
### Convert bytes to base58 encoded string.
|
||||||
|
###
|
||||||
origlen = len(data)
|
origlen = len(data)
|
||||||
data = data.lstrip(b'\0')
|
data = data.lstrip(b'\0')
|
||||||
newlen = len(data)
|
newlen = len(data)
|
||||||
|
@ -37,6 +40,9 @@ def encode(data: bytes) -> str:
|
||||||
|
|
||||||
|
|
||||||
def decode(string: str) -> bytes:
|
def decode(string: str) -> bytes:
|
||||||
|
###
|
||||||
|
### Convert base58 encoded string to bytes.
|
||||||
|
###
|
||||||
origlen = len(string)
|
origlen = len(string)
|
||||||
string = string.lstrip(_alphabet[0])
|
string = string.lstrip(_alphabet[0])
|
||||||
newlen = len(string)
|
newlen = len(string)
|
||||||
|
@ -55,11 +61,16 @@ def decode(string: str) -> bytes:
|
||||||
|
|
||||||
|
|
||||||
def encode_check(data: bytes) -> str:
|
def encode_check(data: bytes) -> str:
|
||||||
|
###
|
||||||
|
### Convert bytes to base58 encoded string, append checksum.
|
||||||
|
###
|
||||||
digest = sha256(sha256(data).digest()).digest()
|
digest = sha256(sha256(data).digest()).digest()
|
||||||
return encode(data + digest[:4])
|
return encode(data + digest[:4])
|
||||||
|
|
||||||
|
|
||||||
def decode_check(string: str) -> bytes:
|
def decode_check(string: str) -> bytes:
|
||||||
|
###
|
||||||
|
### Convert base58 encoded string to bytes and verify checksum.
|
||||||
|
###
|
||||||
result = decode(string)
|
result = decode(string)
|
||||||
result, check = result[:-4], result[-4:]
|
result, check = result[:-4], result[-4:]
|
||||||
digest = sha256(sha256(result).digest()).digest()
|
digest = sha256(sha256(result).digest()).digest()
|
||||||
|
|
|
@ -1,3 +1,9 @@
|
||||||
|
def new(key, msg, digestmod) -> Hmac:
|
||||||
|
###
|
||||||
|
### Creates a HMAC context object.
|
||||||
|
###
|
||||||
|
return Hmac(key, msg, digestmod)
|
||||||
|
|
||||||
class Hmac:
|
class Hmac:
|
||||||
def __init__(self, key, msg, digestmod):
|
def __init__(self, key, msg, digestmod):
|
||||||
self.__digestmod = digestmod
|
self.__digestmod = digestmod
|
||||||
|
@ -12,13 +18,18 @@ class Hmac:
|
||||||
self.update(msg)
|
self.update(msg)
|
||||||
|
|
||||||
def update(self, msg: bytes) -> None:
|
def update(self, msg: bytes) -> None:
|
||||||
|
###
|
||||||
|
### Update the context with data.
|
||||||
|
###
|
||||||
self.__inner.update(msg)
|
self.__inner.update(msg)
|
||||||
|
|
||||||
def digest(self) -> bytes:
|
def digest(self) -> bytes:
|
||||||
|
###
|
||||||
|
### Returns the digest of processed data.
|
||||||
|
###
|
||||||
outer = self.__digestmod()
|
outer = self.__digestmod()
|
||||||
outer.update(bytes((x ^ 0x5C) for x in self.__key))
|
outer.update(bytes((x ^ 0x5C) for x in self.__key))
|
||||||
outer.update(self.__inner.digest())
|
outer.update(self.__inner.digest())
|
||||||
return outer.digest()
|
return outer.digest()
|
||||||
|
|
||||||
def new(key, msg, digestmod) -> Hmac:
|
|
||||||
return Hmac(key, msg, digestmod)
|
|
||||||
|
|
Loading…
Reference in New Issue