Exemplo n.º 1
0
def test_hash_instantiation(name, algcls):
    if name not in md_hash.algorithms_available:
        pytest.skip("not a hash algorithm")
    alg1 = algcls()
    alg2 = md_hash.new(name)
    assert type(alg1) is type(alg2)
    assert alg1.name == alg2.name
Exemplo n.º 2
0
def test_check_against_hashlib_nobuf(algorithm, randbytes):
    buf = randbytes(1024)
    try:
        alg = md_hash.new(algorithm.name, buf)
        ref = hashlib.new(algorithm.name, buf)
    except ValueError as exc:
        # Unsupported hash type.
        pytest.skip(str(exc))
    assert alg.digest() == ref.digest()
Exemplo n.º 3
0
def test_type_accessor():
    def assert_in_bounds(value, lower, higher):
        assert_greater_equal(value, lower)
        assert_less(value, higher)

    for name in md_hash.algorithms_available:
        alg = md_hash.new(name)
        # pylint: disable=protected-access
        test = partial(assert_in_bounds, alg._type, 0, len(MD_NAME))
        test.description = "test_type_accessor(%s)" % name
        yield test
Exemplo n.º 4
0
def test_check_against_hashlib_buf(algorithm, randbytes):
    buf = randbytes(4096)
    try:
        alg = md_hash.new(algorithm.name)
        ref = hashlib.new(algorithm.name)
    except ValueError as exc:
        # Unsupported hash type.
        pytest.skip(str(exc))
    for chunk in make_chunks(buf, 500):
        alg.update(chunk)
        ref.update(chunk)
    assert alg.digest() == ref.digest()
Exemplo n.º 5
0
def test_check_against_hashlib_nobuf():
    for name in md_hash.algorithms_available:
        buf = _rnd(1024)
        try:
            alg = md_hash.new(name, buf)
            ref = hashlib.new(name, buf)
        except ValueError as exc:
            # Unsupported hash type.
            raise SkipTest(str(exc)) from exc
        test = partial(assert_equal, alg.digest(), ref.digest())
        test.description = "check_against_hashlib_nobuf(%s)" % name
        yield test
Exemplo n.º 6
0
def test_copy_hash():
    for name in md_hash.algorithms_available:
        buf0 = _rnd(512)
        buf1 = _rnd(512)
        alg = md_hash.new(name, buf0)
        copy = alg.copy()
        alg.update(buf1)
        copy.update(buf1)
        # Use partial to have the correct name in failed reports (by
        # avoiding late bindings).
        test = partial(assert_equal, alg.digest(), copy.digest())
        test.description = "test_copy_hash(%s)" % name
        yield test
Exemplo n.º 7
0
def test_check_against_hashlib_buf():
    for name in md_hash.algorithms_available:
        buf = _rnd(4096)
        try:
            alg = md_hash.new(name)
            ref = hashlib.new(name)
        except ValueError as exc:
            # Unsupported hash type.
            raise SkipTest(str(exc)) from exc
        for chunk in make_chunks(buf, 500):
            alg.update(chunk)
            ref.update(chunk)
        test = partial(assert_equal, alg.digest(), ref.digest())
        test.description = "check_against_hashlib_buf(%s)" % name
        yield test
Exemplo n.º 8
0
def algorithm(request):
    name = request.param
    return md_hash.new(name)
Exemplo n.º 9
0
 def test_new(self, algorithm, buffer):
     copy = md_hash.new(algorithm.name, buffer)
     algorithm.update(buffer)
     assert algorithm.digest() == copy.digest()
     assert algorithm.hexdigest() == copy.hexdigest()
Exemplo n.º 10
0
 def check_instantiation(fun, name):
     alg1 = fun()
     alg2 = md_hash.new(name)
     assert_equal(type(alg1), type(alg2))
     assert_equal(alg1.name, alg2.name)
Exemplo n.º 11
0
def ripemd_hash(string):
    ripemd = hashlib.new('ripemd160')
    ripemd.update(str.encode(string))
    return ripemd.hexdigest()