ZeroNet/src/util/Electrum.py
ZeroNet 296e4aab57
Fix sslcrypto thread safety (#2454)
* Use sslcrypto instead of pyelliptic and pybitcointools

* Fix CryptMessage

* Support Python 3.4

* Fix user creation

* Get rid of pyelliptic and pybitcointools

* Fix typo

* Delete test file

* Add sslcrypto to tree

* Update sslcrypto

* Add pyaes to src/lib

* Fix typo in tests

* Update sslcrypto version

* Use privatekey_bin instead of privatekey for bytes objects

* Fix sslcrypto

* Fix Benchmark plugin

* Don't calculate the same thing twice

* Only import sslcrypto once

* Handle fallback sslcrypto implementation during tests

* Fix sslcrypto fallback implementation selection

* Fix thread safety

* Add derivation

* Bring split back

* Fix typo

* v3.3

* Fix custom OpenSSL discovery
2020-03-05 17:54:46 +01:00

39 lines
976 B
Python

import hashlib
import struct
# Electrum, the heck?!
def bchr(i):
return struct.pack("B", i)
def encode(val, base, minlen=0):
base, minlen = int(base), int(minlen)
code_string = b"".join([bchr(x) for x in range(256)])
result = b""
while val > 0:
index = val % base
result = code_string[index:index + 1] + result
val //= base
return code_string[0:1] * max(minlen - len(result), 0) + result
def insane_int(x):
x = int(x)
if x < 253:
return bchr(x)
elif x < 65536:
return bchr(253) + encode(x, 256, 2)[::-1]
elif x < 4294967296:
return bchr(254) + encode(x, 256, 4)[::-1]
else:
return bchr(255) + encode(x, 256, 8)[::-1]
def magic(message):
return b"\x18Bitcoin Signed Message:\n" + insane_int(len(message)) + message
def format(message):
return hashlib.sha256(magic(message)).digest()
def dbl_format(message):
return hashlib.sha256(format(message)).digest()