prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / sha1.C
1 // see copyright notice in sha1.h
2 #include "sha1.h"
3
4 void SHA1::addBytes(const uint8_t* input, size_t length)
5 {
6         while (length--) {
7                 m_buffer[m_cursor++] = *input++;
8                 ++m_totalBytes;
9                 if (m_cursor == 64) processBlock();
10         }
11 }
12
13 void SHA1::computeHash(uint8_t *digest)
14 {
15         finalize();
16         memset(digest, 0, 20);
17         for (size_t i = 0; i < 5; ++i) {
18                 // Treat hashValue as a big-endian value.
19                 uint32_t hashValue = m_hash[i];
20                 for (int j = 0; j < 4; ++j) {
21                         digest[4*i + (3-j)] = hashValue & 0xFF;
22                         hashValue >>= 8;
23                 }
24         }
25
26         reset();
27 }
28
29 void SHA1::finalize()
30 {
31         m_buffer[m_cursor++] = 0x80;
32         if (m_cursor > 56) {
33                 while (m_cursor < 64) m_buffer[m_cursor++] = 0x00;
34                 processBlock();
35         }
36
37         for (size_t i = m_cursor; i < 56; ++i)
38                 m_buffer[i] = 0x00;
39
40         uint64_t bits = m_totalBytes*8;
41         for (int i = 0; i < 8; ++i) {
42                 m_buffer[56 + (7-i)] = bits & 0xFF;
43                 bits >>= 8;
44         }
45         m_cursor = 64;
46         processBlock();
47 }
48
49 void SHA1::processBlock()
50 {
51         uint32_t w[80] = { 0 };
52         for (int t = 0; t < 16; ++t)
53                 w[t] =  (m_buffer[t*4] << 24) |
54                 (m_buffer[t*4 + 1] << 16) |
55                 (m_buffer[t*4 + 2] << 8) |
56                 (m_buffer[t*4 + 3]);
57         for (int t = 16; t < 80; ++t)
58                 w[t] = rotateLeft(1, w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16]);
59
60         uint32_t a = m_hash[0], b = m_hash[1];
61         uint32_t c = m_hash[2], d = m_hash[3];
62         uint32_t e = m_hash[4];
63
64         for (int t = 0; t < 80; ++t) {
65                 uint32_t temp = rotateLeft(5, a) + f(t, b, c, d) + e + w[t] + k(t);
66                 e = d;  d = c;
67                 c = rotateLeft(30, b);
68                 b = a;  a = temp;
69         }
70
71         m_hash[0] += a;  m_hash[1] += b;
72         m_hash[2] += c;  m_hash[3] += d;
73         m_hash[4] += e;
74
75         m_cursor = 0;
76 }
77
78 void SHA1::reset()
79 {
80         m_cursor = 0;
81         m_totalBytes = 0;
82         m_hash[0] = 0x67452301;  m_hash[1] = 0xefcdab89;
83         m_hash[2] = 0x98badcfe;  m_hash[3] = 0x10325476;
84         m_hash[4] = 0xc3d2e1f0;
85         memset(m_buffer, 0, sizeof(m_buffer));
86 }
87
88 #ifdef TEST
89 static void expectSHA1(const char *input, int repeat, const char *expected)
90 {
91         SHA1 sha1;
92         const uint8_t *inp = (const uint8_t *)input;
93         int len = strlen(input);
94         for (int i = 0; i < repeat; ++i) sha1.addBytes(inp, len);
95         uint8_t digest[20];  sha1.computeHash(digest);
96         char actual[64], *buffer = actual;
97         for (size_t i = 0; i < 20; ++i) {
98                 snprintf(buffer, 3, "%02X", digest[i]);
99                 buffer += 2;
100         }
101         if( !strcmp(actual, expected) ) return;
102         printf("input: %s, repeat: %d, actual: %s, expected: %s\n",
103                 input, repeat, actual, expected);
104 }
105
106 int main(int ac, char **av)
107 {
108         // Examples taken from sample code in RFC 3174.
109         expectSHA1("abc", 1, "A9993E364706816ABA3E25717850C26C9CD0D89D");
110         expectSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
111                 1, "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");
112         expectSHA1("a",
113                 1000000, "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F");
114         expectSHA1("0123456701234567012345670123456701234567012345670123456701234567",
115                 10, "DEA356A2CDDD90C7A7ECEDC5EBB563934F460452");
116         return 0;
117 }
118 #endif
119