def test_update(self):
        for b, d in self.vectors:
            x = hashlib.ripemd160()
            x.update(b)
            self.assertEqual(x.digest(), unhexlify(d))

        x = hashlib.ripemd160()
        for i in range(8):
            x.update(b'1234567890')
        self.assertEqual(x.digest(), unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb'))
示例#2
0
    def test_update(self):
        x = hashlib.ripemd160()
        self.assertEqual(x.digest(), unhexlify('9c1185a5c5e9fc54612808977ee8f548b2258d31'))

        x = hashlib.ripemd160()
        x.update(b'a')
        self.assertEqual(x.digest(), unhexlify('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe'))

        x = hashlib.ripemd160()
        x.update(b'abc')
        self.assertEqual(x.digest(), unhexlify('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'))

        x = hashlib.ripemd160()
        x.update(b'message digest')
        self.assertEqual(x.digest(), unhexlify('5d0689ef49d2fae572b881b123a85ffa21595f36'))

        x = hashlib.ripemd160()
        x.update(b'abcdefghijklmnopqrstuvwxyz')
        self.assertEqual(x.digest(), unhexlify('f71c27109c692c1b56bbdceb5b9d2865b3708dbc'))

        x = hashlib.ripemd160()
        x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
        self.assertEqual(x.digest(), unhexlify('12a053384a9c0c88e405a06c27dcf49ada62eb2b'))

        x = hashlib.ripemd160()
        x.update(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
        self.assertEqual(x.digest(), unhexlify('b0e20b6e3116640286ed3a87a5713079b21f5189'))

        x = hashlib.ripemd160()
        for i in range(8):
            x.update(b'1234567890')
        self.assertEqual(x.digest(), unhexlify('9b752e45573d4b39f4dbd3323cab82bf63326bfb'))
示例#3
0
async def layout_verify_message(ctx, msg):
    from trezor.messages.Success import Success
    from trezor.crypto.curve import secp256k1
    from trezor.crypto.hashlib import ripemd160, sha256
    from trezor.crypto import base58
    from ..common import address_type
    from ..common import coins
    from ..common.signverify import message_digest

    coin_name = msg.coin_name or 'Bitcoin'
    coin = coins.by_name(coin_name)

    digest = message_digest(coin, msg.message)
    pubkey = secp256k1.verify_recover(msg.signature, digest)

    if not pubkey:
        raise ValueError('Invalid signature')

    raw_address = base58.decode_check(msg.address)
    at, pkh = address_type.split(coin, raw_address)
    pkh2 = ripemd160(sha256(pubkey).digest()).digest()

    if pkh != pkh2:
        raise ValueError('Invalid signature')

    ui.display.clear()
    ui.display.text(10, 30, 'Verifying message', ui.BOLD, ui.LIGHT_GREEN,
                    ui.BG)
    ui.display.text(10, 60, msg.message, ui.MONO, ui.FG, ui.BG)
    ui.display.text(10, 80, msg.address, ui.MONO, ui.FG, ui.BG)

    return Success(message='Message verified')
 def test_digest_multi(self):
     x = hashlib.ripemd160()
     d0 = x.digest()
     d1 = x.digest()
     d2 = x.digest()
     self.assertEqual(d0, d1)
     self.assertEqual(d0, d2)
示例#5
0
def address_p2wpkh_in_p2sh_raw(pubkey: bytes) -> bytes:
    s = bytearray(22)
    s[0] = 0x00  # OP_0
    s[1] = 0x14  # pushing 20 bytes
    s[2:22] = ecdsa_hash_pubkey(pubkey)
    h = sha256(s).digest()
    h = ripemd160(h).digest()
    return h
示例#6
0
def ecdsa_hash_pubkey(pubkey: bytes) -> bytes:
    if pubkey[0] == 0x04:
        ensure(len(pubkey) == 65)  # uncompressed format
    elif pubkey[0] == 0x00:
        ensure(len(pubkey) == 1)   # point at infinity
    else:
        ensure(len(pubkey) == 33)  # compresssed format
    h = sha256(pubkey).digest()
    h = ripemd160(h).digest()
    return h
示例#7
0
def address_from_public_key(pubkey: bytes) -> str:
    """Extracts public key from an address

    Ripple address is in format:
    <1-byte ripple flag> <20-bytes account id> <4-bytes dSHA-256 checksum>

    - 1-byte flag is 0x00 which is 'r' (Ripple uses its own base58 alphabet)
    - 20-bytes account id is a ripemd160(sha256(pubkey))
    - checksum is first 4 bytes of double sha256(data)

    see https://developers.ripple.com/accounts.html#address-encoding
    """
    """Returns the Ripple address created using base58"""
    h = sha256(pubkey).digest()
    h = ripemd160(h).digest()

    address = bytearray()
    address.append(0x00)  # 'r'
    address.extend(h)
    return base58_ripple.encode_check(bytes(address))
示例#8
0
async def verify_message(ctx, msg):
    message = msg.message
    address = msg.address
    signature = msg.signature
    coin_name = msg.coin_name or 'Bitcoin'
    coin = coins.by_name(coin_name)

    await confirm_verify_message(ctx, message)

    digest = message_digest(coin, message)
    pubkey = secp256k1.verify_recover(signature, digest)

    if not pubkey:
        raise wire.FailureError(ProcessError, 'Invalid signature')

    raw_address = base58.decode_check(address)
    _, pkh = address_type.split(coin, raw_address)
    pkh2 = ripemd160(sha256(pubkey).digest()).digest()

    if pkh != pkh2:
        raise wire.FailureError(ProcessError, 'Invalid signature')

    return Success(message='Message verified')
示例#9
0
def sha256_ripemd160_digest(b: bytes) -> bytes:
    h = sha256(b).digest()
    h = ripemd160(h).digest()
    return h
from common import *

from trezor.crypto.hashlib import ripemd160

from trezor.crypto import base58

digestfunc_graphene = lambda x: ripemd160(x).digest()[:4]


class TestCryptoBase58(unittest.TestCase):

    # vectors from https://github.com/bitcoin/bitcoin/blob/master/src/test/data/base58_keys_valid.json
    vectors = [
        ('0065a16059864a2fdbc7c99a4723a8395bc6f188eb',
         '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'),
        ('0574f209f6ea907e2ea48f74fae05782ae8a665257',
         '3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou'),
        ('6f53c0307d6851aa0ce7825ba883c6bd9ad242b486',
         'mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs'),
        ('c46349a418fc4578d10a372b54b45c280cc8c4382f',
         '2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br'),
        ('80eddbdc1168f1daeadbd3e44c1e3f8f5a284c2029f78ad26af98583a499de5b19',
         '5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr'),
        ('8055c9bccb9ed68446d1b75273bbce89d7fe013a8acd1625514420fb2aca1a21c401',
         'Kz6UJmQACJmLtaQj5A3JAge4kVTNQ8gbvXuwbmCj7bsaabudb3RD'),
        ('ef36cb93b9ab1bdabf7fb9f2c04f1b9cc879933530ae7842398eef5a63a56800c2',
         '9213qJab2HNEpMpYNBa7wHGFKKbkDn24jpANDs2huN3yi4J11ko'),
        ('efb9f4892c9e8282028fea1d2667c4dc5213564d41fc5783896a0d843fc15089f301',
         'cTpB4YiyKiBcPxnefsDpbnDxFDffjqJob8wGCEDXxgQ7zQoMXJdH'),
        ('006d23156cbbdcc82a5a47eee4c2c7c583c18b6bf4',
         '1Ax4gZtb7gAit2TivwejZHYtNNLT18PUXJ'),
 def test_digest(self):
     for b, d in self.vectors:
         self.assertEqual(hashlib.ripemd160(b).digest(), unhexlify(d))
示例#12
0
def blake256_ripemd160_digest(b: bytes) -> bytes:
    h = blake256(b).digest()
    h = ripemd160(h).digest()
    return h
示例#13
0
def _ripemd160_32(data: bytes) -> bytes:
    return ripemd160(data).digest()[:4]
示例#14
0
 def digest(self) -> bytes:
     return ripemd160(super().digest()).digest()
def digestfunc_graphene(x):
    return ripemd160(x).digest()[:4]