README: document nit_env.sh
[nit.git] / lib / sha1.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Provides methods to compute the SHA1 hash of a String
16 module sha1
17
18 in "C Header" `{
19
20 /* This code is public-domain - it is based on libcrypt
21 * placed in the public domain by Wei Dai and other contributors.
22 */
23
24 #include <stdint.h>
25 #include <string.h>
26
27 #define HASH_LENGTH 20
28 #define BLOCK_LENGTH 64
29
30 union _buffer {
31 uint8_t b[BLOCK_LENGTH];
32 uint32_t w[BLOCK_LENGTH/4];
33 };
34
35 union _state {
36 uint8_t b[HASH_LENGTH];
37 uint32_t w[HASH_LENGTH/4];
38 };
39
40 typedef struct sha1nfo {
41 union _buffer buffer;
42 uint8_t bufferOffset;
43 union _state state;
44 uint32_t byteCount;
45 uint8_t keyBuffer[BLOCK_LENGTH];
46 uint8_t innerHash[HASH_LENGTH];
47 } sha1nfo;
48
49 /**
50 */
51 void sha1_init(sha1nfo *s);
52 /**
53 */
54 void sha1_writebyte(sha1nfo *s, uint8_t data);
55 /**
56 */
57 void sha1_write(sha1nfo *s, const char *data, size_t len);
58 /**
59 */
60 uint8_t* sha1_result(sha1nfo *s);
61 /**
62 */
63 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength);
64 /**
65 */
66 uint8_t* sha1_resultHmac(sha1nfo *s);
67 `}
68
69 `{
70 #define SHA1_K0 0x5a827999
71 #define SHA1_K20 0x6ed9eba1
72 #define SHA1_K40 0x8f1bbcdc
73 #define SHA1_K60 0xca62c1d6
74
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
81 };
82
83 void sha1_init(sha1nfo *s) {
84 memcpy(s->state.b,sha1InitState,HASH_LENGTH);
85 s->byteCount = 0;
86 s->bufferOffset = 0;
87 }
88
89 uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
90 return ((number << bits) | (number >> (32-bits)));
91 }
92
93 void sha1_hashBlock(sha1nfo *s) {
94 uint8_t i;
95 uint32_t a,b,c,d,e,t;
96
97 a=s->state.w[0];
98 b=s->state.w[1];
99 c=s->state.w[2];
100 d=s->state.w[3];
101 e=s->state.w[4];
102 for (i=0; i<80; i++) {
103 if (i>=16) {
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);
106 }
107 if (i<20) {
108 t = (d ^ (b & (c ^ d))) + SHA1_K0;
109 } else if (i<40) {
110 t = (b ^ c ^ d) + SHA1_K20;
111 } else if (i<60) {
112 t = ((b & c) | (d & (b | c))) + SHA1_K40;
113 } else {
114 t = (b ^ c ^ d) + SHA1_K60;
115 }
116 t+=sha1_rol32(a,5) + e + s->buffer.w[i&15];
117 e=d;
118 d=c;
119 c=sha1_rol32(b,30);
120 b=a;
121 a=t;
122 }
123 s->state.w[0] += a;
124 s->state.w[1] += b;
125 s->state.w[2] += c;
126 s->state.w[3] += d;
127 s->state.w[4] += e;
128 }
129
130 void sha1_addUncounted(sha1nfo *s, uint8_t data) {
131 s->buffer.b[s->bufferOffset ^ 3] = data;
132 s->bufferOffset++;
133 if (s->bufferOffset == BLOCK_LENGTH) {
134 sha1_hashBlock(s);
135 s->bufferOffset = 0;
136 }
137 }
138
139 void sha1_writebyte(sha1nfo *s, uint8_t data) {
140 ++s->byteCount;
141 sha1_addUncounted(s, data);
142 }
143
144 void sha1_write(sha1nfo *s, const char *data, size_t len) {
145 for (;len--;) sha1_writebyte(s, (uint8_t) *data++);
146 }
147
148 void sha1_pad(sha1nfo *s) {
149 // Implement SHA-1 padding (fips180-2 ยง5.1.1)
150
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);
154
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);
164 }
165
166 uint8_t* sha1_result(sha1nfo *s) {
167 int i;
168 // Pad to complete the last block
169 sha1_pad(s);
170
171 // Swap byte order back
172 for (i=0; i<5; i++) {
173 uint32_t a,b;
174 a=s->state.w[i];
175 b=a<<24;
176 b|=(a<<8) & 0x00ff0000;
177 b|=(a>>8) & 0x0000ff00;
178 b|=a>>24;
179 s->state.w[i]=b;
180 }
181
182 // Return pointer to hash (20 characters)
183 return s->state.b;
184 }
185
186 #define HMAC_IPAD 0x36
187 #define HMAC_OPAD 0x5c
188
189 void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength) {
190 uint8_t i;
191 memset(s->keyBuffer, 0, BLOCK_LENGTH);
192 if (keyLength > BLOCK_LENGTH) {
193 // Hash long keys
194 sha1_init(s);
195 for (;keyLength--;) sha1_writebyte(s, *key++);
196 memcpy(s->keyBuffer, sha1_result(s), HASH_LENGTH);
197 } else {
198 // Block length keys are used as is
199 memcpy(s->keyBuffer, key, keyLength);
200 }
201 // Start inner hash
202 sha1_init(s);
203 for (i=0; i<BLOCK_LENGTH; i++) {
204 sha1_writebyte(s, s->keyBuffer[i] ^ HMAC_IPAD);
205 }
206 }
207
208 uint8_t* sha1_resultHmac(sha1nfo *s) {
209 uint8_t i;
210 // Complete inner hash
211 memcpy(s->innerHash,sha1_result(s),HASH_LENGTH);
212 // Calculate outer hash
213 sha1_init(s);
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);
217 }
218 `}
219
220 redef class NativeString
221 private fun sha1_intern(len: Int): NativeString `{
222 sha1nfo s;
223
224 sha1_init(&s);
225 sha1_write(&s, self, len);
226 uint8_t* digest = sha1_result(&s);
227
228 char* digested = malloc(21);
229
230 memcpy(digested, digest, 20);
231
232 digested[20] = '\0';
233
234 return digested;
235 `}
236 end
237
238 redef class String
239
240 # Computes the SHA1 of the receiver
241 #
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
245 # sha1_hexdigest.
246 #
247 # import base64
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]
249 fun sha1: Bytes do
250 return new Bytes(to_cstring.sha1_intern(bytelen), 20, 20)
251 end
252
253 # Computes the SHA1 of the receiver.
254 #
255 # Returns a 40 char String containing the Hexadecimal
256 # Digest in its Char form.
257 #
258 # assert "The quick brown fox jumps over the lazy dog".sha1_hexdigest == "2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"
259 fun sha1_hexdigest: String do return sha1.hexdigest
260 end