本文整理汇总了Python中Crypto.Hash.keccak.new方法的典型用法代码示例。如果您正苦于以下问题:Python keccak.new方法的具体用法?Python keccak.new怎么用?Python keccak.new使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Hash.keccak
的用法示例。
在下文中一共展示了keccak.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checksum_address
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def checksum_address(address):
checksum = '0x'
# Remove '0x' from the address
address = address[2:]
address_byte_array = address.encode('utf-8')
keccak_hash = keccak.new(digest_bits=256)
keccak_hash.update(address_byte_array)
keccak_digest = keccak_hash.hexdigest()
for i in range(len(address)):
address_char = address[i]
keccak_char = keccak_digest[i]
if int(keccak_char, 16) >= 8:
checksum += address_char.upper()
else:
checksum += str(address_char)
return checksum
示例2: __init__
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def __init__(self, x, y):
p = ECPointAffine(bitcoin_curve, x, y)
if not bitcoin_curve.is_on_curve(p):
raise ValueError("The provided (x, y) are not on the secp256k1 curve.")
self.point = p
# RIPEMD-160 of SHA-256
r = hashlib.new('ripemd160')
r.update(hashlib.sha256(bytes(self)).digest())
self.ripe = r.digest()
r = hashlib.new('ripemd160')
r.update(hashlib.sha256(self.compressed_bytes).digest())
self.ripe_compressed = r.digest()
self.keccak = sha3(bytes(self)[1:])
示例3: master_key_from_seed
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def master_key_from_seed(seed):
""" Generates a master key from a provided seed.
Args:
seed (bytes or str): a string of bytes or a hex string
Returns:
HDPrivateKey: the master private key.
"""
S = get_bytes(seed)
I = hmac.new(b"Bitcoin seed", S, hashlib.sha512).digest()
Il, Ir = I[:32], I[32:]
parse_Il = int.from_bytes(Il, 'big')
if parse_Il == 0 or parse_Il >= bitcoin_curve.n:
raise ValueError("Bad seed, resulting in invalid key!")
return HDPrivateKey(key=parse_Il, chain_code=Ir, index=0, depth=0)
示例4: sha1
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def sha1(self):
"""Get SHA1 hash
The SHA (Secure Hash Algorithm) hash functions were designed by the NSA.
SHA-1 is the most established of the existing SHA hash functions and it is
used in a variety of security applications and protocols. However, SHA-1's
collision resistance has been weakening as new attacks are discovered or improved.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha1().output
"6dcd4ce23d88e2ee9568ba546c007c63d9131c1b"
"""
self.state = hashlib.sha1(self._convert_to_bytes()).hexdigest()
return self
示例5: sha2_512_truncate
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def sha2_512_truncate(self, truncate: int = 256):
"""Get SHA2-512/bits hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Args:
truncate (int, optional): The bits to truncate by. Defaults to 256
Returns:
Chepy: The Chepy object.
"""
assert truncate in [256, 224], "Valid truncates are 256, 224"
h = SHA512.new(self._convert_to_bytes(), truncate=str(truncate))
self.state = h.hexdigest()
return self
示例6: md5
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def md5(self):
"""Get MD5 hash
MD5 (Message-Digest 5) is a widely used hash function. It has been used
in a variety of security applications and is also commonly used to check
the integrity of files.<br><br>However, MD5 is not collision resistant and
it isn't suitable for applications like SSL/TLS certificates or digital
signatures that rely on this property.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").md5().output
"7fc56270e7a70fa81a5935b72eacbe29"
"""
h = MD5.new()
h.update(self._convert_to_bytes())
self.state = h.hexdigest()
return self
示例7: shake_256
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def shake_256(self, size: int = 64):
"""Get Shake-256 hash
Shake is an Extendable Output Function (XOF) of the SHA-3 hash algorithm,
part of the Keccak family, allowing for variable output length/size.
Args:
size (int, optional): How many bytes to read, by default 64
Returns:
Chepy: The Chepy object.
"""
h = SHAKE256.new()
h.update(self._convert_to_bytes())
self.state = binascii.hexlify(h.read(size))
return self
示例8: shake_128
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def shake_128(self, size: int = 64):
"""Get Shake-128 hash
Shake is an Extendable Output Function (XOF) of the SHA-3 hash algorithm,
part of the Keccak family, allowing for variable output length/size.
Args:
size (int, optional): How many bytes to read, by default 64
Returns:
Chepy: The Chepy object.
"""
h = SHAKE128.new()
h.update(self._convert_to_bytes())
self.state = binascii.hexlify(h.read(size))
return self
示例9: blake_2s
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def blake_2s(self, bits: int = 256, key: bytes = ""):
"""Get Blake-2s hash
Performs BLAKE2s hashing on the input. BLAKE2s is a flavour of
the BLAKE cryptographic hash function that is optimized for 8- to
32-bit platforms and produces digests of any size between 1 and 32 bytes.
Supports the use of an optional key.
Args:
bits (int, optional): Number of digest bits, by default 256
key (bytes, optional): Encryption secret key, by default ''
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").blake_2s(bits=128, key="key").output
"4e33cc702e9d08c28a5e9691f23bc66a"
"""
assert bits in [256, 160, 128], "Valid bits are 256, 160, 128"
h = BLAKE2s.new(digest_bits=bits, key=key.encode())
h.update(self._convert_to_bytes())
self.state = h.hexdigest()
return self
示例10: test_new_negative
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def test_new_negative(self):
# keccak.new needs digest size
self.assertRaises(TypeError, keccak.new)
h = keccak.new(digest_bits=512)
# Either bits or bytes can be specified
self.assertRaises(TypeError, keccak.new,
digest_bytes=64,
digest_bits=512)
# Range
self.assertRaises(ValueError, keccak.new, digest_bytes=0)
self.assertRaises(ValueError, keccak.new, digest_bytes=1)
self.assertRaises(ValueError, keccak.new, digest_bytes=65)
self.assertRaises(ValueError, keccak.new, digest_bits=0)
self.assertRaises(ValueError, keccak.new, digest_bits=1)
self.assertRaises(ValueError, keccak.new, digest_bits=513)
示例11: test_update_after_digest
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def test_update_after_digest(self):
msg=b("rrrrttt")
# Normally, update() cannot be done after digest()
h = keccak.new(digest_bits=512, data=msg[:4])
dig1 = h.digest()
self.assertRaises(TypeError, h.update, msg[4:])
dig2 = keccak.new(digest_bits=512, data=msg).digest()
# With the proper flag, it is allowed
h = keccak.new(digest_bits=512, data=msg[:4], update_after_digest=True)
self.assertEquals(h.digest(), dig1)
# ... and the subsequent digest applies to the entire message
# up to that point
h.update(msg[4:])
self.assertEquals(h.digest(), dig2)
示例12: __init__
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def __init__(self, data):
# Pad data
pdata = pad(data)
byte_chunks = [pdata[i: i + CHUNK_SIZE] for i in range(0, len(pdata), CHUNK_SIZE)]
# Decompose it into chunks, where each chunk is a collection of numbers
chunks = []
for byte_chunk in byte_chunks:
chunks.append(chunk_to_points(byte_chunk))
# Compute the polynomials representing the ith number in each chunk
polys = [poly_utils.lagrange_interp([chunk[i] for chunk in chunks], list(range(len(chunks)))) for i in range(POINTS_IN_CHUNK)]
# Use the polynomials to extend the chunks
new_chunks = []
for x in range(len(chunks), len(chunks) * 2):
new_chunks.append(points_to_chunk([poly_utils.eval_poly_at(poly, x) for poly in polys]))
# Total length of data including new points
self.length = len(byte_chunks + new_chunks)
self.extended_data = byte_chunks + new_chunks
# Build up the Merkle tree
self.merkle_nodes = merklize(self.extended_data)
assert len(self.merkle_nodes) == 2 * self.length
self.merkle_root = self.merkle_nodes[1]
# Make a Merkle proof for some index
示例13: on_receive_main_block
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def on_receive_main_block(self, block):
# Parent not yet received
if block.parent_hash not in self.blocks:
self.add_to_multiset(self.parentqueue, block.parent_hash, block)
return None
if block.ts > self.ts:
self.add_to_timequeue(block)
return None
self.log("Processing main chain block %s" % to_hex(block.hash[:4]))
self.blocks[block.hash] = block
# Reorg the main chain if new head
if block.number > self.blocks[self.main_chain[-1]].number:
reorging = (block.parent_hash != self.main_chain[-1])
self.change_head(self.main_chain, block)
# Add child record
self.add_to_multiset(self.children, block.parent_hash, block.hash)
# Final steps
self.process_children(block.hash)
self.network.broadcast(self, block)
示例14: _sha3_256
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def _sha3_256(x):
return keccak.new(digest_bits=256, data=x).digest()
示例15: _sha3_512
# 需要导入模块: from Crypto.Hash import keccak [as 别名]
# 或者: from Crypto.Hash.keccak import new [as 别名]
def _sha3_512(x):
return keccak.new(digest_bits=512, data=x).digest()