1 /*******************************************************************************
2 
3         copyright:      Copyright (c) 2006 Tango. All rights reserved
4 
5         license:        BSD style: see doc/license.txt for details
6 
7         version:        Initial release: Feb 2006
8 
9         author:         Regan Heath, Oskar Linde
10 
11         This module implements the SHA-0 Algorithm described by Secure 
12         Hash Standard, FIPS PUB 180
13 
14 *******************************************************************************/
15 
16 module tango.util.digest.Sha0;
17 
18 private import tango.util.digest.Sha01;
19 
20 public  import tango.util.digest.Digest;
21 
22 /*******************************************************************************
23 
24 *******************************************************************************/
25 
26 final class Sha0 : Sha01
27 {
28         /***********************************************************************
29 
30                 Construct an Sha0
31 
32         ***********************************************************************/
33 
34         this() { }
35 
36         /***********************************************************************
37 
38         ***********************************************************************/
39 
40         final protected override void transform(const(ubyte[]) input)
41         {
42                 uint A,B,C,D,E,TEMP;
43                 uint[16] W;
44                 uint s;
45 
46                 bigEndian32(input,W);
47 
48                 A = context[0];
49                 B = context[1];
50                 C = context[2];
51                 D = context[3];
52                 E = context[4];
53 
54                 for(uint t = 0; t < 80; t++) {
55                         s = t & mask;
56                         if (t >= 16) expand(W,s);
57                         TEMP = rotateLeft(A,5) + f(t,B,C,D) + E + W[s] + K[t/20];
58                         E = D; D = C; C = rotateLeft(B,30); B = A; A = TEMP;
59                 }
60 
61                 context[0] += A;
62                 context[1] += B;
63                 context[2] += C;
64                 context[3] += D;
65                 context[4] += E;
66         }
67 
68         /***********************************************************************
69 
70         ***********************************************************************/
71 
72         final static protected void expand(uint[] W, uint s)
73         {
74                 W[s] = W[(s+13)&mask] ^ W[(s+8)&mask] ^ W[(s+2)&mask] ^ W[s];
75         }
76 
77 
78 }
79 
80 
81 /*******************************************************************************
82 
83 *******************************************************************************/
84 
85 debug(UnitTest)
86 {
87         unittest 
88         {
89         __gshared immutable immutable(char)[][] strings = 
90         [
91                 "",
92                 "abc",
93                 "message digest",
94                 "abcdefghijklmnopqrstuvwxyz",
95                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
96                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
97         ];
98 
99         __gshared immutable immutable(char)[][] results = 
100         [
101                 "f96cea198ad1dd5617ac084a3d92c6107708c0ef",
102                 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
103                 "c1b0f222d150ebb9aa36a40cafdc8bcbed830b14",
104                 "b40ce07a430cfd3c033039b9fe9afec95dc1bdcd",
105                 "79e966f7a3a990df33e40e3d7f8f18d2caebadfa",
106                 "4aa29d14d171522ece47bee8957e35a41f3e9cff",
107         ];
108 
109         Sha0 h = new Sha0();
110 
111         foreach (int i, immutable(char)[] s; strings) 
112                 {
113                 h.update(s);
114                 char[] d = h.hexDigest();
115                 assert(d == results[i],":("~s~")("~d~")!=("~results[i]~")");
116                 }
117         }
118 }