1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Lucas Bajolet <r4pass@hotmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Provides methods to compute the SHA1 hash of a String
22 /* This code is public-domain - it is based on libcrypt
23 * placed in the public domain by Wei Dai and other contributors.
29 #define HASH_LENGTH 20
30 #define BLOCK_LENGTH 64
33 uint8_t b[BLOCK_LENGTH];
34 uint32_t w[BLOCK_LENGTH/4];
38 uint8_t b[HASH_LENGTH];
39 uint32_t w[HASH_LENGTH/4];
42 typedef struct sha1nfo {
47 uint8_t keyBuffer[BLOCK_LENGTH];
48 uint8_t innerHash[HASH_LENGTH];
53 void sha1_init(sha1nfo *s);
56 void sha1_writebyte(sha1nfo *s, uint8_t data);
59 void sha1_write(sha1nfo *s, const char *data, size_t len);
62 uint8_t* sha1_result(sha1nfo *s);
65 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength);
68 uint8_t* sha1_resultHmac(sha1nfo *s);
72 #define SHA1_K0 0x5a827999
73 #define SHA1_K20 0x6ed9eba1
74 #define SHA1_K40 0x8f1bbcdc
75 #define SHA1_K60 0xca62c1d6
77 const uint8_t sha1InitState[] = {
78 0x01,0x23,0x45,0x67, // H0
79 0x89,0xab,0xcd,0xef, // H1
80 0xfe,0xdc,0xba,0x98, // H2
81 0x76,0x54,0x32,0x10, // H3
82 0xf0,0xe1,0xd2,0xc3 // H4
85 void sha1_init(sha1nfo *s) {
86 memcpy(s->state.b,sha1InitState,HASH_LENGTH);
91 uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
92 return ((number << bits) | (number >> (32-bits)));
95 void sha1_hashBlock(sha1nfo *s) {
104 for (i=0; i<80; i++) {
106 t = s->buffer.w[(i+13)&15] ^ s->buffer.w[(i+8)&15] ^ s->buffer.w[(i+2)&15] ^ s->buffer.w[i&15];
107 s->buffer.w[i&15] = sha1_rol32(t,1);
110 t = (d ^ (b & (c ^ d))) + SHA1_K0;
112 t = (b ^ c ^ d) + SHA1_K20;
114 t = ((b & c) | (d & (b | c))) + SHA1_K40;
116 t = (b ^ c ^ d) + SHA1_K60;
118 t+=sha1_rol32(a,5) + e + s->buffer.w[i&15];
132 void sha1_addUncounted(sha1nfo *s, uint8_t data) {
133 s->buffer.b[s->bufferOffset ^ 3] = data;
135 if (s->bufferOffset == BLOCK_LENGTH) {
141 void sha1_writebyte(sha1nfo *s, uint8_t data) {
143 sha1_addUncounted(s, data);
146 void sha1_write(sha1nfo *s, const char *data, size_t len) {
147 for (;len--;) sha1_writebyte(s, (uint8_t) *data++);
150 void sha1_pad(sha1nfo *s) {
151 // Implement SHA-1 padding (fips180-2 ยง5.1.1)
153 // Pad with 0x80 followed by 0x00 until the end of the block
154 sha1_addUncounted(s, 0x80);
155 while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);
157 // Append length in the last 8 bytes
158 sha1_addUncounted(s, 0); // We're only using 32 bit lengths
159 sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths
160 sha1_addUncounted(s, 0); // So zero pad the top bits
161 sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8
162 sha1_addUncounted(s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
163 sha1_addUncounted(s, s->byteCount >> 13); // byte.
164 sha1_addUncounted(s, s->byteCount >> 5);
165 sha1_addUncounted(s, s->byteCount << 3);
168 uint8_t* sha1_result(sha1nfo *s) {
170 // Pad to complete the last block
173 // Swap byte order back
174 for (i=0; i<5; i++) {
178 b|=(a<<8) & 0x00ff0000;
179 b|=(a>>8) & 0x0000ff00;
184 // Return pointer to hash (20 characters)
188 #define HMAC_IPAD 0x36
189 #define HMAC_OPAD 0x5c
191 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength) {
193 memset(s->keyBuffer, 0, BLOCK_LENGTH);
194 if (keyLength > BLOCK_LENGTH) {
197 for (;keyLength--;) sha1_writebyte(s, *key++);
198 memcpy(s->keyBuffer, sha1_result(s), HASH_LENGTH);
200 // Block length keys are used as is
201 memcpy(s->keyBuffer, key, keyLength);
205 for (i=0; i<BLOCK_LENGTH; i++) {
206 sha1_writebyte(s, s->keyBuffer[i] ^ HMAC_IPAD);
210 uint8_t* sha1_resultHmac(sha1nfo *s) {
212 // Complete inner hash
213 memcpy(s->innerHash,sha1_result(s),HASH_LENGTH);
214 // Calculate outer hash
216 for (i=0; i<BLOCK_LENGTH; i++) sha1_writebyte(s, s->keyBuffer[i] ^ HMAC_OPAD);
217 for (i=0; i<HASH_LENGTH; i++) sha1_writebyte(s, s->innerHash[i]);
218 return sha1_result(s);
224 # Computes the SHA1 of the receiver
226 # Returns a digest of 20 bytes as a String,
227 # note that all the characters are not necessarily ASCII.
228 # If you want the hex string version of the digest, use
232 # assert "The quick brown fox jumps over the lazy dog".sha1.encode_base64 == "L9ThxnotKPzthJ7hu3bnORuT6xI="
233 fun sha1
: String import String.to_cstring
, String.length
, NativeString.to_s_with_length
`{
237 sha1_write(&s, String_to_cstring(self), String_length(self));
238 uint8_t* digest = sha1_result(&s);
240 char* digested = malloc(21);
242 memcpy(digested, digest, 20);
246 return NativeString_to_s_with_length(digested, 20);
249 # Computes the SHA1 of the receiver.
251 # Returns a 40 char String containing the Hexadecimal
252 # Digest in its Char form.
254 # assert "The quick brown fox jumps over the lazy dog".sha1_to_s == "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"
255 fun sha1_to_s
: String import String.to_cstring
, String.length
, NativeString.to_s_with_length
`{
259 sha1_write(&s, String_to_cstring(self), String_length(self));
260 uint8_t* digest = sha1_result(&s);
262 char* ret_str = malloc(41);
263 char* hexmap = "0123456789ABCDEF";
267 uint8_t q = digest[i];
268 ret_str[i*2] = hexmap[q >> 4];
269 ret_str[(i*2)+1] = hexmap[q & 0x0F];
273 return NativeString_to_s_with_length(ret_str, 40);