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