def test_encode_decode(self): info = sha3(b'hello').digest() seckey = sha3(b'seckey').digest() kp = KeyPair(Secp256k1, seckey) sig = Signature(info, kp) self.assertEqual(sig.pubkey, kp.pubkey) self.assertTrue(sig.verify(info)) bsig = sig.encode() sig2 = Signature() sig2.decode(bsig) self.assertEqual(sig2.pubkey, sig.pubkey) self.assertEqual(sig2.sig, sig.sig) self.assertEqual(sig.algo_cls, sig2.algo_cls)
def _publish_hash(self) -> bytes: """Calculates the publish hash of this `Transaction` (includes `signers` and `signatures`). Returns: A binary hash value. """ return sha3(self.to_bytes('publish')).digest()
def _base_hash(self) -> bytes: """Calculates the base hash of this `Transaction`. Returns: A binary hash value. """ return sha3(self.to_bytes('base')).digest()
def _hash(self) -> bytes: """Calculates the full hash of this `Transaction` (includes `publisher` and `publisher_signatures`). Returns: A binary hash value. """ self.hash = sha3(self.to_bytes('full')).digest() return self.hash
def pk2address(pk, version, n_id=0x00): # Meta data header = "v%i" % (version) # PK hash pk_hash = sha3(pk).hexdigest() # Address address = "_".join([header, pk_hash]) # Checksum checksum = sha3(address.encode()).digest()[:4] address = "_".join([address, checksum.hex()]) print("address: %s" % address) return address
def get_file_hash(file): """Return hash value of file""" h = sha3() with default_storage.open(file, 'rb') as file: chunk = 0 while chunk != b'': chunk = file.read(1024) h.update(chunk) return h.hexdigest()
def get_dict(self, object_to_checksum): checksum = sha3( json.dumps(object_to_checksum, sort_keys=True).encode("utf-8") ).hexdigest() # https://stackoverflow.com/q/5884066 d = {k: v for k, v in vars(self).items() if v is not None} d["checksum"] = checksum d["dateGenerated"] = date.today().strftime(self.DATE_FORMAT) return d
def init_file(self, liste): """ Initialise le fichier :param liste: liste de tuples (key, ip) :return: True si fais, sinon False """ data = {} for tupleD in liste: data[sha3(tupleD[0].encode()).hexdigest()] = (tupleD[1], tupleD[0]) try: with open(liste_membres, "wb") as file: dump(data, file) return True except: return False
def __init__(self, tx, nonce, seed_hash): self.ver = __version__ self.time = int(time.time() * 1000) self.relayer = ip self.tx = tx tx_hashes = [t.hash for t in self.tx] self.nonce = str(nonce) # Hash uuid = ";".join( [self.ver, str(self.time), self.relayer, self.nonce, *tx_hashes]) self.hash = sha3(uuid.encode()).hexdigest() print(self.hash)
def __init__(self, sender, receiver, amount, sk): self.ver = __version__ self.time = int(time.time() * 1000) self.relayer = ip self.sender = sender self.receiver = receiver self.amount = Decimal(amount) # Create hash, and signature uuid = ";".join([ self.ver, str(self.time), self.relayer, self.sender, self.receiver, str(self.amount), ]) self.hash = sha3(uuid.encode()).hexdigest() self.sig = sphincs.sign(self.hash.encode(), bytes.fromhex(sk)).hex()
def crypto_wacky_digest(msg_digest, salt): """ never roll your own... except as a backup plan and fun learning experience! random amount of multiple hashing types to impose novelty restraint """ shaken_salt = shake256(salt.encode()).digest(16) # randomized iteration count for _ in range(int(salt[-4:])): # for each up to 10 iterations; 1 in 10 no skip for _ in range(int(salt[-1])): # salted 512 chacha stream cipher with permuted input block copy msg_digest = hexlify(blake(msg_digest, salt=shaken_salt).digest()) for _ in range(int(salt[-2])): # keccak 512 sponge construction msg_digest = sha3(msg_digest).digest() for _ in range(int(salt[-3])): # standard sha512 msg_digest = sha512(msg_digest).digest() return msg_digest
def add_entry(self, liste): """ Rajoute une entrée :param liste: liste de tuples (key, ip) :return: True si fais, sinon False """ data = {} try: with open(liste_membres, "rb") as file: data = load(file) for tupleD in liste: data[sha3(tupleD[0].encode()).hexdigest()] = (tupleD[1], tupleD[0]) with open(liste_membres, "wb") as file: dump(data, file) return True except: return False
def test_sha256(self): sha = 'f420b28b56ce97e52adf4778a72b622c3e91115445026cf6e641459ec478dae8' self.assertEqual(sha, sha3(self.privkey).hexdigest())
def verify_checksum(stub, checksum): return sha3(stub.encode()).digest()[:4].hex() == checksum
from hashlib import sha3_256 as sha3 from base64 import b64encode from pyost.algorithm import Ed25519, Secp256k1 from pyost.signature import KeyPair if __name__ == '__main__': text = b'hello' info = sha3(text).digest() print(info.hex()) for algo in [Ed25519, Secp256k1]: kp = KeyPair(algo) print(kp.algo_cls.__int__()) print(kp.seckey.hex()) print(kp.pubkey.hex()) sig = kp.sign(info) print(sig.pubkey.hex()) print(sig.sig.hex()) print(b64encode(sig.pubkey)) print(b64encode(sig.sig))
def get_message_hash(*, message): """Return has of given message""" return sha3(sort_and_encode(message)).digest().hex()
def make_id(description, *ints): return sha3(''.join(encode(i, 256).rjust(32, '\x00') for i in ints) + description).digest()
def calculate_checksum(data): sha3 = hashlib.sha3() sha3.update(normalize(data).encode('utf-8')) return base64.b64encode(sha3.digest()).decode('utf-8').rstrip('=')
OFFSET = n // 2 RANGO = (OFFSET, OFFSET + REPEAT) s = time() for number in range(*RANGO): Q = fast_times(number) print(number, Q) print((time() - s) / REPEAT) msg = "signature!" sign_msg = b"\x19Ethereum Signed Message:\n" \ + str(len(msg)).encode('utf8') \ + msg.encode('utf8') print(msg) hash_ = int('0x' + sha3(sign_msg).hexdigest(), 0) pk = int(4e0) pubKey = fast_times(pk) signature = sign_hash(hash_, pk) validation = verify_sign_hash(hash_, pubKey, signature) print("signature:\n\t", signature) print("valid: ", validation) pubKey_bytes = tup2bytes(tup2bytes(pubKey)) address = '0x' + sha3(pubKey_bytes).digest()[-20:].hex() print("hexas") print("address\n", address) print("signature\n0x00", tup2bytes(signature).hex()) ''' print("\nTests:") priv = 15
def make_id(description, *ints): return sha3(''.join(encode(i,256).rjust(32,'\x00')for i in ints)+description).digest()
def hash(text): return sha3(str(text).encode('utf-8')).hexdigest()
def test_sign_and_verify(self): info = sha3(bytearray([1, 2, 3, 4])).digest() sig = Secp256k1.sign(info, self.privkey) self.assertTrue(Secp256k1.verify(info, self.pubkey, sig)) self.assertFalse( Secp256k1.verify(info, self.pubkey, bytearray([5, 6, 7, 8] * 16)))