29 #ifndef _TINY_SHA1_HPP_
30 #define _TINY_SHA1_HPP_
50 typedef uint32_t digest32_t[5];
51 typedef uint8_t digest8_t[20];
52 inline static uint32_t LeftRotate(uint32_t value,
size_t count) {
53 return (value << count) ^ (value >> (32-count));
58 const SHA1& operator = (
const SHA1& s) {
59 memcpy(m_digest, s.m_digest, 5 *
sizeof(uint32_t));
60 memcpy(m_block, s.m_block, 64);
61 m_blockByteIndex = s.m_blockByteIndex;
62 m_byteCount = s.m_byteCount;
66 m_digest[0] = 0x67452301;
67 m_digest[1] = 0xEFCDAB89;
68 m_digest[2] = 0x98BADCFE;
69 m_digest[3] = 0x10325476;
70 m_digest[4] = 0xC3D2E1F0;
75 SHA1& processByte(uint8_t octet) {
76 this->m_block[this->m_blockByteIndex++] = octet;
78 if(m_blockByteIndex == 64) {
79 this->m_blockByteIndex = 0;
84 SHA1& processBlock(
const void*
const start,
const void*
const end) {
85 const uint8_t* begin =
static_cast<const uint8_t*
>(start);
86 const uint8_t* finish =
static_cast<const uint8_t*
>(end);
87 while(begin != finish) {
93 SHA1& processBytes(
const void*
const data,
size_t len) {
94 const uint8_t* block =
static_cast<const uint8_t*
>(data);
95 processBlock(block, block + len);
98 const uint32_t* getDigest(digest32_t digest) {
99 size_t bitCount = this->m_byteCount * 8;
101 if (this->m_blockByteIndex > 56) {
102 while (m_blockByteIndex != 0) {
105 while (m_blockByteIndex < 56) {
109 while (m_blockByteIndex < 56) {
117 processByte(
static_cast<unsigned char>((bitCount>>24) & 0xFF));
118 processByte(
static_cast<unsigned char>((bitCount>>16) & 0xFF));
119 processByte(
static_cast<unsigned char>((bitCount>>8 ) & 0xFF));
120 processByte(
static_cast<unsigned char>((bitCount) & 0xFF));
122 memcpy(digest, m_digest, 5 *
sizeof(uint32_t));
125 const uint8_t* getDigestBytes(digest8_t digest) {
129 digest[di++] = ((d32[0] >> 24) & 0xFF);
130 digest[di++] = ((d32[0] >> 16) & 0xFF);
131 digest[di++] = ((d32[0] >> 8) & 0xFF);
132 digest[di++] = ((d32[0]) & 0xFF);
134 digest[di++] = ((d32[1] >> 24) & 0xFF);
135 digest[di++] = ((d32[1] >> 16) & 0xFF);
136 digest[di++] = ((d32[1] >> 8) & 0xFF);
137 digest[di++] = ((d32[1]) & 0xFF);
139 digest[di++] = ((d32[2] >> 24) & 0xFF);
140 digest[di++] = ((d32[2] >> 16) & 0xFF);
141 digest[di++] = ((d32[2] >> 8) & 0xFF);
142 digest[di++] = ((d32[2]) & 0xFF);
144 digest[di++] = ((d32[3] >> 24) & 0xFF);
145 digest[di++] = ((d32[3] >> 16) & 0xFF);
146 digest[di++] = ((d32[3] >> 8) & 0xFF);
147 digest[di++] = ((d32[3]) & 0xFF);
149 digest[di++] = ((d32[4] >> 24) & 0xFF);
150 digest[di++] = ((d32[4] >> 16) & 0xFF);
151 digest[di++] = ((d32[4] >> 8) & 0xFF);
152 digest[di++] = ((d32[4]) & 0xFF);
157 void processBlock() {
159 for (
size_t i = 0; i < 16; i++) {
160 w[i] = (m_block[i*4 + 0] << 24);
161 w[i] |= (m_block[i*4 + 1] << 16);
162 w[i] |= (m_block[i*4 + 2] << 8);
163 w[i] |= (m_block[i*4 + 3]);
165 for (
size_t i = 16; i < 80; i++) {
166 w[i] = LeftRotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
169 uint32_t a = m_digest[0];
170 uint32_t b = m_digest[1];
171 uint32_t c = m_digest[2];
172 uint32_t d = m_digest[3];
173 uint32_t e = m_digest[4];
175 for (std::size_t i=0; i<80; ++i) {
180 f = (b & c) | (~b & d);
186 f = (b & c) | (b & d) | (c & d);
192 uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i];
195 c = LeftRotate(b, 30);
209 size_t m_blockByteIndex;
A tiny SHA1 algorithm implementation used internally in the Crow server (specifically in crow/websock...
Definition: TinySHA1.hpp:48
Here is defined the SHA1 class.