rubysl-digest 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +7 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/ext/rubysl/digest/bubblebabble/.gitignore +2 -0
  7. data/ext/rubysl/digest/bubblebabble/bubblebabble.c +147 -0
  8. data/ext/rubysl/digest/bubblebabble/bubblebabble.h +2 -0
  9. data/ext/rubysl/digest/bubblebabble/depend +3 -0
  10. data/ext/rubysl/digest/bubblebabble/extconf.h +4 -0
  11. data/ext/rubysl/digest/bubblebabble/extconf.rb +6 -0
  12. data/ext/rubysl/digest/defs.h +19 -0
  13. data/ext/rubysl/digest/digest.c +660 -0
  14. data/ext/rubysl/digest/digest.h +32 -0
  15. data/ext/rubysl/digest/extconf.rb +10 -0
  16. data/ext/rubysl/digest/md5/.gitignore +2 -0
  17. data/ext/rubysl/digest/md5/extconf.rb +29 -0
  18. data/ext/rubysl/digest/md5/md5.c +422 -0
  19. data/ext/rubysl/digest/md5/md5.h +80 -0
  20. data/ext/rubysl/digest/md5/md5init.c +40 -0
  21. data/ext/rubysl/digest/md5/md5ossl.c +9 -0
  22. data/ext/rubysl/digest/md5/md5ossl.h +13 -0
  23. data/ext/rubysl/digest/rmd160/.gitignore +2 -0
  24. data/ext/rubysl/digest/rmd160/extconf.rb +28 -0
  25. data/ext/rubysl/digest/rmd160/rmd160.c +457 -0
  26. data/ext/rubysl/digest/rmd160/rmd160.h +56 -0
  27. data/ext/rubysl/digest/rmd160/rmd160init.c +40 -0
  28. data/ext/rubysl/digest/rmd160/rmd160ossl.c +8 -0
  29. data/ext/rubysl/digest/rmd160/rmd160ossl.h +19 -0
  30. data/ext/rubysl/digest/sha1/.gitignore +2 -0
  31. data/ext/rubysl/digest/sha1/extconf.rb +28 -0
  32. data/ext/rubysl/digest/sha1/sha1.c +269 -0
  33. data/ext/rubysl/digest/sha1/sha1.h +39 -0
  34. data/ext/rubysl/digest/sha1/sha1init.c +40 -0
  35. data/ext/rubysl/digest/sha1/sha1ossl.c +10 -0
  36. data/ext/rubysl/digest/sha1/sha1ossl.h +20 -0
  37. data/ext/rubysl/digest/sha2/.gitignore +2 -0
  38. data/ext/rubysl/digest/sha2/extconf.rb +24 -0
  39. data/ext/rubysl/digest/sha2/sha2.c +919 -0
  40. data/ext/rubysl/digest/sha2/sha2.h +109 -0
  41. data/ext/rubysl/digest/sha2/sha2init.c +52 -0
  42. data/lib/digest/bubblebabble.rb +1 -0
  43. data/lib/digest/hmac.rb +302 -0
  44. data/lib/digest/md5.rb +23 -0
  45. data/lib/digest/rmd160.rb +1 -0
  46. data/lib/digest/sha1.rb +23 -0
  47. data/lib/digest/sha2.rb +74 -0
  48. data/lib/digest.rb +1 -0
  49. data/lib/rubysl/digest/digest.rb +88 -0
  50. data/lib/{rubysl-digest → rubysl/digest}/version.rb +1 -1
  51. data/lib/rubysl/digest.rb +2 -0
  52. data/rubysl-digest.gemspec +25 -17
  53. data/spec/hexencode_spec.rb +30 -0
  54. data/spec/md5/append_spec.rb +6 -0
  55. data/spec/md5/block_length_spec.rb +11 -0
  56. data/spec/md5/digest_bang_spec.rb +12 -0
  57. data/spec/md5/digest_length_spec.rb +11 -0
  58. data/spec/md5/digest_spec.rb +31 -0
  59. data/spec/md5/equal_spec.rb +37 -0
  60. data/spec/md5/file_spec.rb +42 -0
  61. data/spec/md5/hexdigest_bang_spec.rb +13 -0
  62. data/spec/md5/hexdigest_spec.rb +31 -0
  63. data/spec/md5/inspect_spec.rb +11 -0
  64. data/spec/md5/length_spec.rb +7 -0
  65. data/spec/md5/reset_spec.rb +14 -0
  66. data/spec/md5/shared/constants.rb +16 -0
  67. data/spec/md5/shared/length.rb +8 -0
  68. data/spec/md5/shared/sample.rb +15 -0
  69. data/spec/md5/shared/update.rb +7 -0
  70. data/spec/md5/size_spec.rb +7 -0
  71. data/spec/md5/to_s_spec.rb +21 -0
  72. data/spec/md5/update_spec.rb +6 -0
  73. data/spec/sha1/digest_spec.rb +19 -0
  74. data/spec/sha1/file_spec.rb +42 -0
  75. data/spec/sha1/shared/constants.rb +16 -0
  76. data/spec/sha256/append_spec.rb +6 -0
  77. data/spec/sha256/block_length_spec.rb +11 -0
  78. data/spec/sha256/digest_bang_spec.rb +12 -0
  79. data/spec/sha256/digest_length_spec.rb +11 -0
  80. data/spec/sha256/digest_spec.rb +31 -0
  81. data/spec/sha256/equal_spec.rb +36 -0
  82. data/spec/sha256/file_spec.rb +42 -0
  83. data/spec/sha256/hexdigest_bang_spec.rb +13 -0
  84. data/spec/sha256/hexdigest_spec.rb +31 -0
  85. data/spec/sha256/inspect_spec.rb +11 -0
  86. data/spec/sha256/length_spec.rb +7 -0
  87. data/spec/sha256/reset_spec.rb +14 -0
  88. data/spec/sha256/shared/constants.rb +16 -0
  89. data/spec/sha256/shared/length.rb +8 -0
  90. data/spec/sha256/shared/update.rb +7 -0
  91. data/spec/sha256/size_spec.rb +7 -0
  92. data/spec/sha256/to_s_spec.rb +20 -0
  93. data/spec/sha256/update_spec.rb +6 -0
  94. data/spec/sha384/append_spec.rb +6 -0
  95. data/spec/sha384/block_length_spec.rb +11 -0
  96. data/spec/sha384/digest_bang_spec.rb +12 -0
  97. data/spec/sha384/digest_length_spec.rb +11 -0
  98. data/spec/sha384/digest_spec.rb +31 -0
  99. data/spec/sha384/equal_spec.rb +36 -0
  100. data/spec/sha384/file_spec.rb +42 -0
  101. data/spec/sha384/hexdigest_bang_spec.rb +13 -0
  102. data/spec/sha384/hexdigest_spec.rb +31 -0
  103. data/spec/sha384/inspect_spec.rb +11 -0
  104. data/spec/sha384/length_spec.rb +7 -0
  105. data/spec/sha384/reset_spec.rb +14 -0
  106. data/spec/sha384/shared/constants.rb +17 -0
  107. data/spec/sha384/shared/length.rb +8 -0
  108. data/spec/sha384/shared/update.rb +7 -0
  109. data/spec/sha384/size_spec.rb +7 -0
  110. data/spec/sha384/to_s_spec.rb +20 -0
  111. data/spec/sha384/update_spec.rb +6 -0
  112. data/spec/sha512/append_spec.rb +6 -0
  113. data/spec/sha512/block_length_spec.rb +11 -0
  114. data/spec/sha512/digest_bang_spec.rb +12 -0
  115. data/spec/sha512/digest_length_spec.rb +11 -0
  116. data/spec/sha512/digest_spec.rb +31 -0
  117. data/spec/sha512/equal_spec.rb +36 -0
  118. data/spec/sha512/file_spec.rb +42 -0
  119. data/spec/sha512/hexdigest_bang_spec.rb +13 -0
  120. data/spec/sha512/hexdigest_spec.rb +31 -0
  121. data/spec/sha512/inspect_spec.rb +11 -0
  122. data/spec/sha512/length_spec.rb +7 -0
  123. data/spec/sha512/reset_spec.rb +14 -0
  124. data/spec/sha512/shared/constants.rb +16 -0
  125. data/spec/sha512/shared/length.rb +8 -0
  126. data/spec/sha512/shared/update.rb +7 -0
  127. data/spec/sha512/size_spec.rb +7 -0
  128. data/spec/sha512/to_s_spec.rb +20 -0
  129. data/spec/sha512/update_spec.rb +6 -0
  130. metadata +283 -88
  131. data/lib/rubysl-digest.rb +0 -7
@@ -0,0 +1,269 @@
1
+ /* $NetBSD: sha1.c,v 1.2 2001/03/22 09:51:48 agc Exp $ */
2
+ /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
3
+ /* $RoughId: sha1.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
4
+ /* $Id: sha1.c 11708 2007-02-12 23:01:19Z shyouhei $ */
5
+
6
+ /*
7
+ * SHA-1 in C
8
+ * By Steve Reid <steve@edmweb.com>
9
+ * 100% Public Domain
10
+ *
11
+ * Test Vectors (from FIPS PUB 180-1)
12
+ * "abc"
13
+ * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
14
+ * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
15
+ * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
16
+ * A million repetitions of "a"
17
+ * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
18
+ */
19
+
20
+ #include "sha1.h"
21
+
22
+ #define SHA1HANDSOFF /* Copies data before messing with it. */
23
+
24
+ #if defined(_KERNEL) || defined(_STANDALONE)
25
+ #include <sys/param.h>
26
+ #include <sys/systm.h>
27
+ #define _DIAGASSERT(x) (void)0
28
+ #else
29
+ /* #include "namespace.h" */
30
+ #include <assert.h>
31
+ #include <string.h>
32
+ #endif
33
+
34
+ #ifndef _DIAGASSERT
35
+ #define _DIAGASSERT(cond) assert(cond)
36
+ #endif
37
+
38
+ /*
39
+ * XXX Kludge until there is resolution regarding mem*() functions
40
+ * XXX in the kernel.
41
+ */
42
+ #if defined(_KERNEL) || defined(_STANDALONE)
43
+ #define memcpy(s, d, l) bcopy((d), (s), (l))
44
+ #endif
45
+
46
+ #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
47
+
48
+ /*
49
+ * blk0() and blk() perform the initial expand.
50
+ * I got the idea of expanding during the round function from SSLeay
51
+ */
52
+ #ifndef WORDS_BIGENDIAN
53
+ # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
54
+ |(rol(block->l[i],8)&0x00FF00FF))
55
+ #else
56
+ # define blk0(i) block->l[i]
57
+ #endif
58
+ #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
59
+ ^block->l[(i+2)&15]^block->l[i&15],1))
60
+
61
+ /*
62
+ * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
63
+ */
64
+ #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
65
+ #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
66
+ #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
67
+ #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
68
+ #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
69
+
70
+
71
+ typedef union {
72
+ uint8_t c[64];
73
+ uint32_t l[16];
74
+ } CHAR64LONG16;
75
+
76
+ #ifdef __sparc_v9__
77
+ void do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
78
+ void do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
79
+ void do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
80
+ void do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *);
81
+
82
+ #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
83
+ #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
84
+ #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
85
+ #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
86
+ #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
87
+
88
+ void
89
+ do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
90
+ {
91
+ nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
92
+ nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
93
+ nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
94
+ nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15);
95
+ nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
96
+ }
97
+
98
+ void
99
+ do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
100
+ {
101
+ nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
102
+ nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
103
+ nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
104
+ nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35);
105
+ nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
106
+ }
107
+
108
+ void
109
+ do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
110
+ {
111
+ nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
112
+ nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
113
+ nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
114
+ nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55);
115
+ nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
116
+ }
117
+
118
+ void
119
+ do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LONG16 *block)
120
+ {
121
+ nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
122
+ nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
123
+ nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
124
+ nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75);
125
+ nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
126
+ }
127
+ #endif
128
+
129
+ /*
130
+ * Hash a single 512-bit block. This is the core of the algorithm.
131
+ */
132
+ void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
133
+ {
134
+ uint32_t a, b, c, d, e;
135
+ CHAR64LONG16 *block;
136
+
137
+ #ifdef SHA1HANDSOFF
138
+ CHAR64LONG16 workspace;
139
+ #endif
140
+
141
+ _DIAGASSERT(buffer != 0);
142
+ _DIAGASSERT(state != 0);
143
+
144
+ #ifdef SHA1HANDSOFF
145
+ block = &workspace;
146
+ (void)memcpy(block, buffer, 64);
147
+ #else
148
+ block = (CHAR64LONG16 *)(void *)buffer;
149
+ #endif
150
+
151
+ /* Copy context->state[] to working vars */
152
+ a = state[0];
153
+ b = state[1];
154
+ c = state[2];
155
+ d = state[3];
156
+ e = state[4];
157
+
158
+ #ifdef __sparc_v9__
159
+ do_R01(&a, &b, &c, &d, &e, block);
160
+ do_R2(&a, &b, &c, &d, &e, block);
161
+ do_R3(&a, &b, &c, &d, &e, block);
162
+ do_R4(&a, &b, &c, &d, &e, block);
163
+ #else
164
+ /* 4 rounds of 20 operations each. Loop unrolled. */
165
+ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
166
+ R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
167
+ R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
168
+ R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
169
+ R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
170
+ R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
171
+ R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
172
+ R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
173
+ R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
174
+ R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
175
+ R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
176
+ R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
177
+ R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
178
+ R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
179
+ R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
180
+ R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
181
+ R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
182
+ R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
183
+ R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
184
+ R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
185
+ #endif
186
+
187
+ /* Add the working vars back into context.state[] */
188
+ state[0] += a;
189
+ state[1] += b;
190
+ state[2] += c;
191
+ state[3] += d;
192
+ state[4] += e;
193
+
194
+ /* Wipe variables */
195
+ a = b = c = d = e = 0;
196
+ }
197
+
198
+
199
+ /*
200
+ * SHA1_Init - Initialize new context
201
+ */
202
+ void SHA1_Init(SHA1_CTX *context)
203
+ {
204
+
205
+ _DIAGASSERT(context != 0);
206
+
207
+ /* SHA1 initialization constants */
208
+ context->state[0] = 0x67452301;
209
+ context->state[1] = 0xEFCDAB89;
210
+ context->state[2] = 0x98BADCFE;
211
+ context->state[3] = 0x10325476;
212
+ context->state[4] = 0xC3D2E1F0;
213
+ context->count[0] = context->count[1] = 0;
214
+ }
215
+
216
+
217
+ /*
218
+ * Run your data through this.
219
+ */
220
+ void SHA1_Update(SHA1_CTX *context, const uint8_t *data, size_t len)
221
+ {
222
+ uint32_t i, j;
223
+
224
+ _DIAGASSERT(context != 0);
225
+ _DIAGASSERT(data != 0);
226
+
227
+ j = context->count[0];
228
+ if ((context->count[0] += len << 3) < j)
229
+ context->count[1] += (len>>29)+1;
230
+ j = (j >> 3) & 63;
231
+ if ((j + len) > 63) {
232
+ (void)memcpy(&context->buffer[j], data, (i = 64-j));
233
+ SHA1_Transform(context->state, context->buffer);
234
+ for ( ; i + 63 < len; i += 64)
235
+ SHA1_Transform(context->state, &data[i]);
236
+ j = 0;
237
+ } else {
238
+ i = 0;
239
+ }
240
+ (void)memcpy(&context->buffer[j], &data[i], len - i);
241
+ }
242
+
243
+
244
+ /*
245
+ * Add padding and return the message digest.
246
+ */
247
+ void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
248
+ {
249
+ size_t i;
250
+ uint8_t finalcount[8];
251
+
252
+ _DIAGASSERT(digest != 0);
253
+ _DIAGASSERT(context != 0);
254
+
255
+ for (i = 0; i < 8; i++) {
256
+ finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
257
+ >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
258
+ }
259
+ SHA1_Update(context, (const uint8_t *)"\200", 1);
260
+ while ((context->count[0] & 504) != 448)
261
+ SHA1_Update(context, (const uint8_t *)"\0", 1);
262
+ SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */
263
+
264
+ if (digest) {
265
+ for (i = 0; i < 20; i++)
266
+ digest[i] = (uint8_t)
267
+ ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
268
+ }
269
+ }
@@ -0,0 +1,39 @@
1
+ /* $NetBSD: sha1.h,v 1.2 1998/05/29 22:55:44 thorpej Exp $ */
2
+ /* $RoughId: sha1.h,v 1.3 2002/02/24 08:14:32 knu Exp $ */
3
+ /* $Id: sha1.h 11708 2007-02-12 23:01:19Z shyouhei $ */
4
+
5
+ /*
6
+ * SHA-1 in C
7
+ * By Steve Reid <steve@edmweb.com>
8
+ * 100% Public Domain
9
+ */
10
+
11
+ #ifndef _SYS_SHA1_H_
12
+ #define _SYS_SHA1_H_
13
+
14
+ #include "defs.h"
15
+
16
+ typedef struct {
17
+ uint32_t state[5];
18
+ uint32_t count[2];
19
+ uint8_t buffer[64];
20
+ } SHA1_CTX;
21
+
22
+ #ifdef RUBY
23
+ /* avoid name clash */
24
+ #define SHA1_Transform rb_Digest_SHA1_Transform
25
+ #define SHA1_Init rb_Digest_SHA1_Init
26
+ #define SHA1_Update rb_Digest_SHA1_Update
27
+ #define SHA1_Finish rb_Digest_SHA1_Finish
28
+ #endif
29
+
30
+ void SHA1_Transform _((uint32_t state[5], const uint8_t buffer[64]));
31
+ void SHA1_Init _((SHA1_CTX *context));
32
+ void SHA1_Update _((SHA1_CTX *context, const uint8_t *data, size_t len));
33
+ void SHA1_Finish _((SHA1_CTX *context, uint8_t digest[20]));
34
+
35
+ #define SHA1_BLOCK_LENGTH 64
36
+ #define SHA1_DIGEST_LENGTH 20
37
+ #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
38
+
39
+ #endif /* _SYS_SHA1_H_ */
@@ -0,0 +1,40 @@
1
+ /* $RoughId: sha1init.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
2
+ /* $Id: sha1init.c 19020 2008-09-01 07:48:53Z nobu $ */
3
+
4
+ #include "digest.h"
5
+ #if defined(HAVE_OPENSSL_SHA_H)
6
+ #include "sha1ossl.h"
7
+ #else
8
+ #include "sha1.h"
9
+ #endif
10
+
11
+ static const rb_digest_metadata_t sha1 = {
12
+ RUBY_DIGEST_API_VERSION,
13
+ SHA1_DIGEST_LENGTH,
14
+ SHA1_BLOCK_LENGTH,
15
+ sizeof(SHA1_CTX),
16
+ (rb_digest_hash_init_func_t)SHA1_Init,
17
+ (rb_digest_hash_update_func_t)SHA1_Update,
18
+ (rb_digest_hash_finish_func_t)SHA1_Finish,
19
+ };
20
+
21
+ /*
22
+ * A class for calculating message digests using the SHA-1 Secure Hash
23
+ * Algorithm by NIST (the US' National Institute of Standards and
24
+ * Technology), described in FIPS PUB 180-1.
25
+ */
26
+ void
27
+ Init_sha1()
28
+ {
29
+ VALUE mDigest, cDigest_Base, cDigest_SHA1;
30
+
31
+ rb_require("digest");
32
+
33
+ mDigest = rb_path2class("Digest");
34
+ cDigest_Base = rb_path2class("Digest::Base");
35
+
36
+ cDigest_SHA1 = rb_define_class_under(mDigest, "SHA1", cDigest_Base);
37
+
38
+ rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
39
+ Data_Wrap_Struct(rb_cObject, 0, 0, (void *)&sha1));
40
+ }
@@ -0,0 +1,10 @@
1
+ /* $Id: sha1ossl.c 11902 2007-02-27 10:08:39Z knu $ */
2
+
3
+ #include "defs.h"
4
+ #include "sha1ossl.h"
5
+
6
+ void
7
+ SHA1_Finish(SHA1_CTX *ctx, char *buf)
8
+ {
9
+ SHA1_Final((unsigned char *)buf, ctx);
10
+ }
@@ -0,0 +1,20 @@
1
+ /* $Id: sha1ossl.h 11708 2007-02-12 23:01:19Z shyouhei $ */
2
+
3
+ #ifndef SHA1OSSL_H_INCLUDED
4
+ #define SHA1OSSL_H_INCLUDED
5
+
6
+ #include <stddef.h>
7
+ #include <openssl/sha.h>
8
+
9
+ #define SHA1_CTX SHA_CTX
10
+
11
+ #ifdef SHA_BLOCK_LENGTH
12
+ #define SHA1_BLOCK_LENGTH SHA_BLOCK_LENGTH
13
+ #else
14
+ #define SHA1_BLOCK_LENGTH SHA_CBLOCK
15
+ #endif
16
+ #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
17
+
18
+ void SHA1_Finish(SHA1_CTX *ctx, char *buf);
19
+
20
+ #endif
@@ -0,0 +1,2 @@
1
+ Makefile
2
+ *.log
@@ -0,0 +1,24 @@
1
+ # $RoughId: extconf.rb,v 1.4 2001/08/14 19:54:51 knu Exp $
2
+ # $Id$
3
+
4
+ require "mkmf"
5
+
6
+ $defs << "-DHAVE_CONFIG_H"
7
+ $INCFLAGS << " -I$(srcdir)/.."
8
+
9
+ $objs = [
10
+ "sha2.#{$OBJEXT}",
11
+ "sha2init.#{$OBJEXT}",
12
+ ]
13
+
14
+ have_header("sys/cdefs.h")
15
+
16
+ have_header("inttypes.h")
17
+
18
+ have_header("unistd.h")
19
+
20
+ $preload = %w[digest]
21
+
22
+ if have_type("uint64_t", "defs.h", $defs.join(' '))
23
+ create_makefile("digest/sha2/sha2")
24
+ end