1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Provides methods to compute the SHA1 hash of a String
20 /* This code is public-domain - it is based on libcrypt
21 * placed in the public domain by Wei Dai and other contributors.
27 #define HASH_LENGTH 20
28 #define BLOCK_LENGTH 64
31 uint8_t b[BLOCK_LENGTH];
32 uint32_t w[BLOCK_LENGTH/4];
36 uint8_t b[HASH_LENGTH];
37 uint32_t w[HASH_LENGTH/4];
40 typedef struct sha1nfo {
45 uint8_t keyBuffer[BLOCK_LENGTH];
46 uint8_t innerHash[HASH_LENGTH];
51 void sha1_init(sha1nfo *s);
54 void sha1_writebyte(sha1nfo *s, uint8_t data);
57 void sha1_write(sha1nfo *s, const char *data, size_t len);
60 uint8_t* sha1_result(sha1nfo *s);
63 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength);
66 uint8_t* sha1_resultHmac(sha1nfo *s);
70 #define SHA1_K0 0x5a827999
71 #define SHA1_K20 0x6ed9eba1
72 #define SHA1_K40 0x8f1bbcdc
73 #define SHA1_K60 0xca62c1d6
75 const uint8_t sha1InitState[] = {
76 0x01,0x23,0x45,0x67, // H0
77 0x89,0xab,0xcd,0xef, // H1
78 0xfe,0xdc,0xba,0x98, // H2
79 0x76,0x54,0x32,0x10, // H3
80 0xf0,0xe1,0xd2,0xc3 // H4
83 void sha1_init(sha1nfo *s) {
84 memcpy(s->state.b,sha1InitState,HASH_LENGTH);
89 uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
90 return ((number << bits) | (number >> (32-bits)));
93 void sha1_hashBlock(sha1nfo *s) {
102 for (i=0; i<80; i++) {
104 t = s->buffer.w[(i+13)&15] ^ s->buffer.w[(i+8)&15] ^ s->buffer.w[(i+2)&15] ^ s->buffer.w[i&15];
105 s->buffer.w[i&15] = sha1_rol32(t,1);
108 t = (d ^ (b & (c ^ d))) + SHA1_K0;
110 t = (b ^ c ^ d) + SHA1_K20;
112 t = ((b & c) | (d & (b | c))) + SHA1_K40;
114 t = (b ^ c ^ d) + SHA1_K60;
116 t+=sha1_rol32(a,5) + e + s->buffer.w[i&15];
130 void sha1_addUncounted(sha1nfo *s, uint8_t data) {
131 s->buffer.b[s->bufferOffset ^ 3] = data;
133 if (s->bufferOffset == BLOCK_LENGTH) {
139 void sha1_writebyte(sha1nfo *s, uint8_t data) {
141 sha1_addUncounted(s, data);
144 void sha1_write(sha1nfo *s, const char *data, size_t len) {
145 for (;len--;) sha1_writebyte(s, (uint8_t) *data++);
148 void sha1_pad(sha1nfo *s) {
149 // Implement SHA-1 padding (fips180-2 ยง5.1.1)
151 // Pad with 0x80 followed by 0x00 until the end of the block
152 sha1_addUncounted(s, 0x80);
153 while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);
155 // Append length in the last 8 bytes
156 sha1_addUncounted(s, 0); // We're only using 32 bit lengths
157 sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths
158 sha1_addUncounted(s, 0); // So zero pad the top bits
159 sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8
160 sha1_addUncounted(s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
161 sha1_addUncounted(s, s->byteCount >> 13); // byte.
162 sha1_addUncounted(s, s->byteCount >> 5);
163 sha1_addUncounted(s, s->byteCount << 3);
166 uint8_t* sha1_result(sha1nfo *s) {
168 // Pad to complete the last block
171 // Swap byte order back
172 for (i=0; i<5; i++) {
176 b|=(a<<8) & 0x00ff0000;
177 b|=(a>>8) & 0x0000ff00;
182 // Return pointer to hash (20 characters)
186 #define HMAC_IPAD 0x36
187 #define HMAC_OPAD 0x5c
189 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength) {
191 memset(s->keyBuffer, 0, BLOCK_LENGTH);
192 if (keyLength > BLOCK_LENGTH) {
195 for (;keyLength--;) sha1_writebyte(s, *key++);
196 memcpy(s->keyBuffer, sha1_result(s), HASH_LENGTH);
198 // Block length keys are used as is
199 memcpy(s->keyBuffer, key, keyLength);
203 for (i=0; i<BLOCK_LENGTH; i++) {
204 sha1_writebyte(s, s->keyBuffer[i] ^ HMAC_IPAD);
208 uint8_t* sha1_resultHmac(sha1nfo *s) {
210 // Complete inner hash
211 memcpy(s->innerHash,sha1_result(s),HASH_LENGTH);
212 // Calculate outer hash
214 for (i=0; i<BLOCK_LENGTH; i++) sha1_writebyte(s, s->keyBuffer[i] ^ HMAC_OPAD);
215 for (i=0; i<HASH_LENGTH; i++) sha1_writebyte(s, s->innerHash[i]);
216 return sha1_result(s);
220 redef class NativeString
221 private fun sha1_intern
(len
: Int): NativeString `{
225 sha1_write(&s, self, len);
226 uint8_t* digest = sha1_result(&s);
228 char* digested = malloc(21);
230 memcpy(digested, digest, 20);
240 # Computes the SHA1 of the receiver
242 # Returns a digest of 20 bytes as a NativeString,
243 # note that all the characters are not necessarily ASCII.
244 # If you want the hex string version of the digest, use
248 # assert "The quick brown fox jumps over the lazy dog".sha1 == [0x2Fu8, 0xD4u8, 0xE1u8, 0xC6u8, 0x7Au8, 0x2Du8, 0x28u8, 0xFCu8, 0xEDu8, 0x84u8, 0x9Eu8, 0xE1u8, 0xBBu8, 0x76u8, 0xE7u8, 0x39u8, 0x1Bu8, 0x93u8, 0xEBu8, 0x12u8]
250 return new Bytes(to_cstring
.sha1_intern
(bytelen
), 20, 20)
253 # Computes the SHA1 of the receiver.
255 # Returns a 40 char String containing the Hexadecimal
256 # Digest in its Char form.
258 # assert "The quick brown fox jumps over the lazy dog".sha1_hexdigest == "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"
259 fun sha1_hexdigest
: String do return sha1
.hexdigest