MCPcopy Create free account
hub / github.com/ElementsProject/lightning / sha1digest

Function sha1digest

connectd/sha1.c:46–191  ·  view source on GitHub ↗

* sha1digest: https://github.com/CTrabant/teeny-sha1 * * Calculate the SHA-1 value for supplied data buffer and generate a * text representation in hexadecimal. * * Based on https://github.com/jinqiangshou/EncryptionLibrary, credit * goes to @jinqiangshou, all new bugs are mine. * * @input: * data -- data to be hashed * databytes -- bytes in data buffer to be hashed * * @out

Source from the content-addressed store, hash-verified

44 * @return: 0 on success and non-zero on error.
45 ******************************************************************************/
46int
47sha1digest(uint8_t *digest, const uint8_t *data, size_t databytes)
48{
49#define SHA1ROTATELEFT(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
50
51 uint32_t W[80];
52 uint32_t H[] = {0x67452301,
53 0xEFCDAB89,
54 0x98BADCFE,
55 0x10325476,
56 0xC3D2E1F0};
57 uint32_t a;
58 uint32_t b;
59 uint32_t c;
60 uint32_t d;
61 uint32_t e;
62 uint32_t f = 0;
63 uint32_t k = 0;
64
65 uint32_t idx;
66 uint32_t lidx;
67 uint32_t widx;
68 uint32_t didx = 0;
69
70 int32_t wcount;
71 uint32_t temp;
72 uint64_t databits = ((uint64_t)databytes) * 8;
73 uint32_t loopcount = (databytes + 8) / 64 + 1;
74 uint32_t tailbytes = 64 * loopcount - databytes;
75 uint8_t datatail[128] = {0};
76
77 if (!digest)
78 return -1;
79
80 if (!data)
81 return -1;
82
83 /* Pre-processing of data tail (includes padding to fill out 512-bit chunk):
84 Add bit '1' to end of message (big-endian)
85 Add 64-bit message length in bits at very end (big-endian) */
86 datatail[0] = 0x80;
87 datatail[tailbytes - 8] = (uint8_t) (databits >> 56 & 0xFF);
88 datatail[tailbytes - 7] = (uint8_t) (databits >> 48 & 0xFF);
89 datatail[tailbytes - 6] = (uint8_t) (databits >> 40 & 0xFF);
90 datatail[tailbytes - 5] = (uint8_t) (databits >> 32 & 0xFF);
91 datatail[tailbytes - 4] = (uint8_t) (databits >> 24 & 0xFF);
92 datatail[tailbytes - 3] = (uint8_t) (databits >> 16 & 0xFF);
93 datatail[tailbytes - 2] = (uint8_t) (databits >> 8 & 0xFF);
94 datatail[tailbytes - 1] = (uint8_t) (databits >> 0 & 0xFF);
95
96 /* Process each 512-bit chunk */
97 for (lidx = 0; lidx < loopcount; lidx++)
98 {
99 /* Compute all elements in W */
100 memset (W, 0, 80 * sizeof (uint32_t));
101
102 /* Break 512-bit chunk into sixteen 32-bit, big endian words */
103 for (widx = 0; widx <= 15; widx++)

Callers 1

websocket_accept_strFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected