Coverage for src/sha1.mys : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1c"""header-before-namespace
2#include "../../src/hash/sha1.hpp"
3"""
5class Sha1:
6 c"struct sha1_t m_hasher;"
8 func __init__(self):
9 c"sha1_init(&m_hasher);"
11 func update(self, data: bytes):
12 """Update the hash with given data.
14 """
16 c"sha1_update(&m_hasher, data.m_bytes->data(), data.m_bytes->size());"
18 func digest(self) -> bytes:
19 """Returns the hash.
21 """
23 digest = bytes(20)
25 c"sha1_digest(&m_hasher, digest.m_bytes->data());"
27 return digest
29 func reset(self):
30 """Reset the hasher state.
32 """
34 c"sha1_init(&m_hasher);"
36func sha1(data: bytes) -> bytes:
37 """Returns the hash of given data.
39 """
41 hasher = Sha1()
42 hasher.update(data)
44 return hasher.digest()
46test vectors():
47 datas = [
48 (
49 b"",
50 b"\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55"
51 b"\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09"
52 ),
53 (
54 b"abc",
55 b"\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e"
56 b"\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d"
57 ),
58 (
59 b"The quick brown fox jumps over the lazy dog",
60 b"\x2f\xd4\xe1\xc6\x7a\x2d\x28\xfc\xed\x84"
61 b"\x9e\xe1\xbb\x76\xe7\x39\x1b\x93\xeb\x12"
62 ),
63 (
64 b"The quick brown fox jumps over the lazy cog",
65 b"\xde\x9f\x2c\x7f\xd2\x5e\x1b\x3a\xfa\xd3"
66 b"\xe8\x5a\x0b\xd1\x7d\x9b\x10\x0d\xb4\xb3"
67 ),
68 (
69 b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
70 b"\x13\xd9\x56\x03\x3d\x9a\xf4\x49\xbf\xe2"
71 b"\xc4\xef\x78\xc1\x7c\x20\x46\x9c\x4b\xf1"
72 ),
73 (
74 b"abcdefghbcdefghicdefghijdefghijkefghijklfgh"
75 b"ijklmghijklmnhijklmnoijklmnopjklmnopqklmnop"
76 b"qrlmnopqrsmnopqrstnopqrstu",
77 b"\xa4\x9b\x24\x46\xa0\x2c\x64\x5b\xf4\x19"
78 b"\xf9\x95\xb6\x70\x91\x25\x3a\x04\xa2\x59"
79 )
80 ]
82 for data, hashed in datas:
83 hasher = Sha1()
84 hasher.update(data)
85 assert hasher.digest() == hashed
87 for data, hashed in datas:
88 assert sha1(data) == hashed
90test multiple_updates():
91 hasher = Sha1()
93 for i in range(400):
94 hasher.update(b"1")
96 assert hasher.digest() == (b"\x7f\xdd\xab\x82\x28\xdf\x0f\x39\x77\xed"
97 b"\xf7\x3b\xb8\x06\x86\x11\x59\xad\x89\xd1")
99test reset():
100 data = b"The quick brown fox jumps over the lazy dog"
101 hashed = (b"\x2f\xd4\xe1\xc6\x7a\x2d\x28\xfc\xed\x84"
102 b"\x9e\xe1\xbb\x76\xe7\x39\x1b\x93\xeb\x12")
104 hasher = Sha1()
106 hasher.update(data)
107 hasher.update(data)
108 assert hasher.digest() != hashed
110 hasher.reset()
111 hasher.update(data)
112 assert hasher.digest() == hashed