comparison dcrypt/crypto/hashes/SHA1.d @ 2:71aae178f89a

Added copy() to hash functions. Modified some code style.
author Thomas Dixon <reikon@reikon.us>
date Wed, 13 Aug 2008 22:01:19 -0400
parents 0e08791a1418
children a5789a7b3b3b
comparison
equal deleted inserted replaced
1:483e4467b5f6 2:71aae178f89a
10 10
11 public import dcrypt.crypto.Hash; 11 public import dcrypt.crypto.Hash;
12 12
13 /** Implementation of SHA-1. */ 13 /** Implementation of SHA-1. */
14 class SHA1 : Hash { 14 class SHA1 : Hash {
15 private uint h0, h1, h2, h3, h4; 15 protected uint h0, h1, h2, h3, h4;
16 16
17 this (void[] input_=null) { 17 this (void[] input_=null) {
18 reset(); 18 reset();
19 super(input_); 19 super(input_);
20 } 20 }
159 h2 = 0x98badcfeu; 159 h2 = 0x98badcfeu;
160 h3 = 0x10325476u; 160 h3 = 0x10325476u;
161 h4 = 0xc3d2e1f0u; 161 h4 = 0xc3d2e1f0u;
162 } 162 }
163 163
164 SHA1 copy() {
165 SHA1 h = new SHA1(buffer[0..index]);
166 h.h0 = h0;
167 h.h1 = h1;
168 h.h2 = h2;
169 h.h3 = h3;
170 h.h4 = h4;
171 return h;
172 }
173
164 version (UnitTest) { 174 version (UnitTest) {
165 unittest { 175 unittest {
166 static const char[][] test_inputs = [ 176 static const char[][] test_inputs = [
167 "", 177 "",
168 "abc", 178 "abc",
187 foreach (uint i, char[] input; test_inputs) { 197 foreach (uint i, char[] input; test_inputs) {
188 for (int j = 0; j < test_repeat[i]; j++) 198 for (int j = 0; j < test_repeat[i]; j++)
189 h.update(input); 199 h.update(input);
190 char[] digest = h.hexDigest(); 200 char[] digest = h.hexDigest();
191 assert(digest == test_results[i], 201 assert(digest == test_results[i],
192 h.name()~": ("~digest~") != ("~test_results[i]~")"); 202 h.name~": ("~digest~") != ("~test_results[i]~")");
193 } 203 }
194 } 204 }
195 } 205 }
196 } 206 }