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