| 1 |
{******************************************************************************} |
| 2 |
{* DCPcrypt v2.0 written by David Barton ([email protected]) **********} |
| 3 |
{******************************************************************************} |
| 4 |
{* A binary compatible implementation of RipeMD-128 ***************************} |
| 5 |
{******************************************************************************} |
| 6 |
{* Copyright (c) 2002 David Barton *} |
| 7 |
{* Permission is hereby granted, free of charge, to any person obtaining a *} |
| 8 |
{* copy of this software and associated documentation files (the "Software"), *} |
| 9 |
{* to deal in the Software without restriction, including without limitation *} |
| 10 |
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *} |
| 11 |
{* and/or sell copies of the Software, and to permit persons to whom the *} |
| 12 |
{* Software is furnished to do so, subject to the following conditions: *} |
| 13 |
{* *} |
| 14 |
{* The above copyright notice and this permission notice shall be included in *} |
| 15 |
{* all copies or substantial portions of the Software. *} |
| 16 |
{* *} |
| 17 |
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *} |
| 18 |
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *} |
| 19 |
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *} |
| 20 |
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *} |
| 21 |
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *} |
| 22 |
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *} |
| 23 |
{* DEALINGS IN THE SOFTWARE. *} |
| 24 |
{******************************************************************************} |
| 25 |
unit DCPripemd128; |
| 26 |
|
| 27 |
{$MODE Delphi} |
| 28 |
|
| 29 |
interface |
| 30 |
uses |
| 31 |
Classes, Sysutils, DCPcrypt2, DCPconst; |
| 32 |
|
| 33 |
type |
| 34 |
TDCP_ripemd128= class(TDCP_hash) |
| 35 |
protected |
| 36 |
LenHi, LenLo: longword; |
| 37 |
Index: DWord; |
| 38 |
CurrentHash: array[0..3] of DWord; |
| 39 |
HashBuffer: array[0..63] of byte; |
| 40 |
procedure Compress; |
| 41 |
public |
| 42 |
class function GetId: integer; override; |
| 43 |
class function GetAlgorithm: string; override; |
| 44 |
class function GetHashSize: integer; override; |
| 45 |
class function SelfTest: boolean; override; |
| 46 |
procedure Init; override; |
| 47 |
procedure Burn; override; |
| 48 |
procedure Update(const Buffer; Size: longword); override; |
| 49 |
procedure Final(var Digest); override; |
| 50 |
end; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
{******************************************************************************} |
| 55 |
{******************************************************************************} |
| 56 |
implementation |
| 57 |
{$R-}{$Q-} |
| 58 |
|
| 59 |
procedure TDCP_ripemd128.Compress; |
| 60 |
var |
| 61 |
X: array[0..15] of DWord; |
| 62 |
a, aa, b, bb, c, cc, d, dd, t: dword; |
| 63 |
begin |
| 64 |
dcpFillChar(X, SizeOf(X), 0); |
| 65 |
Move(HashBuffer,X,Sizeof(X)); |
| 66 |
a:= CurrentHash[0]; aa:= a; |
| 67 |
b:= CurrentHash[1]; bb:= b; |
| 68 |
c:= CurrentHash[2]; cc:= c; |
| 69 |
d:= CurrentHash[3]; dd:= d; |
| 70 |
|
| 71 |
t:= a + (b xor c xor d) + X[ 0]; a:= (t shl 11) or (t shr (32-11)); |
| 72 |
t:= d + (a xor b xor c) + X[ 1]; d:= (t shl 14) or (t shr (32-14)); |
| 73 |
t:= c + (d xor a xor b) + X[ 2]; c:= (t shl 15) or (t shr (32-15)); |
| 74 |
t:= b + (c xor d xor a) + X[ 3]; b:= (t shl 12) or (t shr (32-12)); |
| 75 |
t:= a + (b xor c xor d) + X[ 4]; a:= (t shl 5) or (t shr (32-5)); |
| 76 |
t:= d + (a xor b xor c) + X[ 5]; d:= (t shl 8) or (t shr (32-8)); |
| 77 |
t:= c + (d xor a xor b) + X[ 6]; c:= (t shl 7) or (t shr (32-7)); |
| 78 |
t:= b + (c xor d xor a) + X[ 7]; b:= (t shl 9) or (t shr (32-9)); |
| 79 |
t:= a + (b xor c xor d) + X[ 8]; a:= (t shl 11) or (t shr (32-11)); |
| 80 |
t:= d + (a xor b xor c) + X[ 9]; d:= (t shl 13) or (t shr (32-13)); |
| 81 |
t:= c + (d xor a xor b) + X[10]; c:= (t shl 14) or (t shr (32-14)); |
| 82 |
t:= b + (c xor d xor a) + X[11]; b:= (t shl 15) or (t shr (32-15)); |
| 83 |
t:= a + (b xor c xor d) + X[12]; a:= (t shl 6) or (t shr (32-6)); |
| 84 |
t:= d + (a xor b xor c) + X[13]; d:= (t shl 7) or (t shr (32-7)); |
| 85 |
t:= c + (d xor a xor b) + X[14]; c:= (t shl 9) or (t shr (32-9)); |
| 86 |
t:= b + (c xor d xor a) + X[15]; b:= (t shl 8) or (t shr (32-8)); |
| 87 |
|
| 88 |
t:= a + ((b and c) or (not b and d)) + X[ 7]+$5A827999; a:= (t shl 7) or (t shr (32-7)); |
| 89 |
t:= d + ((a and b) or (not a and c)) + X[ 4]+$5A827999; d:= (t shl 6) or (t shr (32-6)); |
| 90 |
t:= c + ((d and a) or (not d and b)) + X[13]+$5A827999; c:= (t shl 8) or (t shr (32-8)); |
| 91 |
t:= b + ((c and d) or (not c and a)) + X[ 1]+$5A827999; b:= (t shl 13) or (t shr (32-13)); |
| 92 |
t:= a + ((b and c) or (not b and d)) + X[10]+$5A827999; a:= (t shl 11) or (t shr (32-11)); |
| 93 |
t:= d + ((a and b) or (not a and c)) + X[ 6]+$5A827999; d:= (t shl 9) or (t shr (32-9)); |
| 94 |
t:= c + ((d and a) or (not d and b)) + X[15]+$5A827999; c:= (t shl 7) or (t shr (32-7)); |
| 95 |
t:= b + ((c and d) or (not c and a)) + X[ 3]+$5A827999; b:= (t shl 15) or (t shr (32-15)); |
| 96 |
t:= a + ((b and c) or (not b and d)) + X[12]+$5A827999; a:= (t shl 7) or (t shr (32-7)); |
| 97 |
t:= d + ((a and b) or (not a and c)) + X[ 0]+$5A827999; d:= (t shl 12) or (t shr (32-12)); |
| 98 |
t:= c + ((d and a) or (not d and b)) + X[ 9]+$5A827999; c:= (t shl 15) or (t shr (32-15)); |
| 99 |
t:= b + ((c and d) or (not c and a)) + X[ 5]+$5A827999; b:= (t shl 9) or (t shr (32-9)); |
| 100 |
t:= a + ((b and c) or (not b and d)) + X[ 2]+$5A827999; a:= (t shl 11) or (t shr (32-11)); |
| 101 |
t:= d + ((a and b) or (not a and c)) + X[14]+$5A827999; d:= (t shl 7) or (t shr (32-7)); |
| 102 |
t:= c + ((d and a) or (not d and b)) + X[11]+$5A827999; c:= (t shl 13) or (t shr (32-13)); |
| 103 |
t:= b + ((c and d) or (not c and a)) + X[ 8]+$5A827999; b:= (t shl 12) or (t shr (32-12)); |
| 104 |
|
| 105 |
t:= a + ((b or not c) xor d) + X[ 3]+$6ED9EBA1; a:= (t shl 11) or (t shr (32-11)); |
| 106 |
t:= d + ((a or not b) xor c) + X[10]+$6ED9EBA1; d:= (t shl 13) or (t shr (32-13)); |
| 107 |
t:= c + ((d or not a) xor b) + X[14]+$6ED9EBA1; c:= (t shl 6) or (t shr (32-6)); |
| 108 |
t:= b + ((c or not d) xor a) + X[ 4]+$6ED9EBA1; b:= (t shl 7) or (t shr (32-7)); |
| 109 |
t:= a + ((b or not c) xor d) + X[ 9]+$6ED9EBA1; a:= (t shl 14) or (t shr (32-14)); |
| 110 |
t:= d + ((a or not b) xor c) + X[15]+$6ED9EBA1; d:= (t shl 9) or (t shr (32-9)); |
| 111 |
t:= c + ((d or not a) xor b) + X[ 8]+$6ED9EBA1; c:= (t shl 13) or (t shr (32-13)); |
| 112 |
t:= b + ((c or not d) xor a) + X[ 1]+$6ED9EBA1; b:= (t shl 15) or (t shr (32-15)); |
| 113 |
t:= a + ((b or not c) xor d) + X[ 2]+$6ED9EBA1; a:= (t shl 14) or (t shr (32-14)); |
| 114 |
t:= d + ((a or not b) xor c) + X[ 7]+$6ED9EBA1; d:= (t shl 8) or (t shr (32-8)); |
| 115 |
t:= c + ((d or not a) xor b) + X[ 0]+$6ED9EBA1; c:= (t shl 13) or (t shr (32-13)); |
| 116 |
t:= b + ((c or not d) xor a) + X[ 6]+$6ED9EBA1; b:= (t shl 6) or (t shr (32-6)); |
| 117 |
t:= a + ((b or not c) xor d) + X[13]+$6ED9EBA1; a:= (t shl 5) or (t shr (32-5)); |
| 118 |
t:= d + ((a or not b) xor c) + X[11]+$6ED9EBA1; d:= (t shl 12) or (t shr (32-12)); |
| 119 |
t:= c + ((d or not a) xor b) + X[ 5]+$6ED9EBA1; c:= (t shl 7) or (t shr (32-7)); |
| 120 |
t:= b + ((c or not d) xor a) + X[12]+$6ED9EBA1; b:= (t shl 5) or (t shr (32-5)); |
| 121 |
|
| 122 |
t:= a + ((b and d) or (c and not d)) + X[ 1]+$8F1BBCDC; a:= (t shl 11) or (t shr (32-11)); |
| 123 |
t:= d + ((a and c) or (b and not c)) + X[ 9]+$8F1BBCDC; d:= (t shl 12) or (t shr (32-12)); |
| 124 |
t:= c + ((d and b) or (a and not b)) + X[11]+$8F1BBCDC; c:= (t shl 14) or (t shr (32-14)); |
| 125 |
t:= b + ((c and a) or (d and not a)) + X[10]+$8F1BBCDC; b:= (t shl 15) or (t shr (32-15)); |
| 126 |
t:= a + ((b and d) or (c and not d)) + X[ 0]+$8F1BBCDC; a:= (t shl 14) or (t shr (32-14)); |
| 127 |
t:= d + ((a and c) or (b and not c)) + X[ 8]+$8F1BBCDC; d:= (t shl 15) or (t shr (32-15)); |
| 128 |
t:= c + ((d and b) or (a and not b)) + X[12]+$8F1BBCDC; c:= (t shl 9) or (t shr (32-9)); |
| 129 |
t:= b + ((c and a) or (d and not a)) + X[ 4]+$8F1BBCDC; b:= (t shl 8) or (t shr (32-8)); |
| 130 |
t:= a + ((b and d) or (c and not d)) + X[13]+$8F1BBCDC; a:= (t shl 9) or (t shr (32-9)); |
| 131 |
t:= d + ((a and c) or (b and not c)) + X[ 3]+$8F1BBCDC; d:= (t shl 14) or (t shr (32-14)); |
| 132 |
t:= c + ((d and b) or (a and not b)) + X[ 7]+$8F1BBCDC; c:= (t shl 5) or (t shr (32-5)); |
| 133 |
t:= b + ((c and a) or (d and not a)) + X[15]+$8F1BBCDC; b:= (t shl 6) or (t shr (32-6)); |
| 134 |
t:= a + ((b and d) or (c and not d)) + X[14]+$8F1BBCDC; a:= (t shl 8) or (t shr (32-8)); |
| 135 |
t:= d + ((a and c) or (b and not c)) + X[ 5]+$8F1BBCDC; d:= (t shl 6) or (t shr (32-6)); |
| 136 |
t:= c + ((d and b) or (a and not b)) + X[ 6]+$8F1BBCDC; c:= (t shl 5) or (t shr (32-5)); |
| 137 |
t:= b + ((c and a) or (d and not a)) + X[ 2]+$8F1BBCDC; b:= (t shl 12) or (t shr (32-12)); |
| 138 |
|
| 139 |
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 5]+$50A28BE6; aa:= (t shl 8) or (t shr (32-8)); |
| 140 |
t:= dd + ((aa and cc) or (bb and not cc)) + X[14]+$50A28BE6; dd:= (t shl 9) or (t shr (32-9)); |
| 141 |
t:= cc + ((dd and bb) or (aa and not bb)) + X[ 7]+$50A28BE6; cc:= (t shl 9) or (t shr (32-9)); |
| 142 |
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 0]+$50A28BE6; bb:= (t shl 11) or (t shr (32-11)); |
| 143 |
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 9]+$50A28BE6; aa:= (t shl 13) or (t shr (32-13)); |
| 144 |
t:= dd + ((aa and cc) or (bb and not cc)) + X[ 2]+$50A28BE6; dd:= (t shl 15) or (t shr (32-15)); |
| 145 |
t:= cc + ((dd and bb) or (aa and not bb)) + X[11]+$50A28BE6; cc:= (t shl 15) or (t shr (32-15)); |
| 146 |
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 4]+$50A28BE6; bb:= (t shl 5) or (t shr (32-5)); |
| 147 |
t:= aa + ((bb and dd) or (cc and not dd)) + X[13]+$50A28BE6; aa:= (t shl 7) or (t shr (32-7)); |
| 148 |
t:= dd + ((aa and cc) or (bb and not cc)) + X[ 6]+$50A28BE6; dd:= (t shl 7) or (t shr (32-7)); |
| 149 |
t:= cc + ((dd and bb) or (aa and not bb)) + X[15]+$50A28BE6; cc:= (t shl 8) or (t shr (32-8)); |
| 150 |
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 8]+$50A28BE6; bb:= (t shl 11) or (t shr (32-11)); |
| 151 |
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 1]+$50A28BE6; aa:= (t shl 14) or (t shr (32-14)); |
| 152 |
t:= dd + ((aa and cc) or (bb and not cc)) + X[10]+$50A28BE6; dd:= (t shl 14) or (t shr (32-14)); |
| 153 |
t:= cc + ((dd and bb) or (aa and not bb)) + X[ 3]+$50A28BE6; cc:= (t shl 12) or (t shr (32-12)); |
| 154 |
t:= bb + ((cc and aa) or (dd and not aa)) + X[12]+$50A28BE6; bb:= (t shl 6) or (t shr (32-6)); |
| 155 |
|
| 156 |
t:= aa + ((bb or not cc) xor dd) + X[ 6]+$5C4DD124; aa:= (t shl 9) or (t shr (32-9)); |
| 157 |
t:= dd + ((aa or not bb) xor cc) + X[11]+$5C4DD124; dd:= (t shl 13) or (t shr (32-13)); |
| 158 |
t:= cc + ((dd or not aa) xor bb) + X[ 3]+$5C4DD124; cc:= (t shl 15) or (t shr (32-15)); |
| 159 |
t:= bb + ((cc or not dd) xor aa) + X[ 7]+$5C4DD124; bb:= (t shl 7) or (t shr (32-7)); |
| 160 |
t:= aa + ((bb or not cc) xor dd) + X[ 0]+$5C4DD124; aa:= (t shl 12) or (t shr (32-12)); |
| 161 |
t:= dd + ((aa or not bb) xor cc) + X[13]+$5C4DD124; dd:= (t shl 8) or (t shr (32-8)); |
| 162 |
t:= cc + ((dd or not aa) xor bb) + X[ 5]+$5C4DD124; cc:= (t shl 9) or (t shr (32-9)); |
| 163 |
t:= bb + ((cc or not dd) xor aa) + X[10]+$5C4DD124; bb:= (t shl 11) or (t shr (32-11)); |
| 164 |
t:= aa + ((bb or not cc) xor dd) + X[14]+$5C4DD124; aa:= (t shl 7) or (t shr (32-7)); |
| 165 |
t:= dd + ((aa or not bb) xor cc) + X[15]+$5C4DD124; dd:= (t shl 7) or (t shr (32-7)); |
| 166 |
t:= cc + ((dd or not aa) xor bb) + X[ 8]+$5C4DD124; cc:= (t shl 12) or (t shr (32-12)); |
| 167 |
t:= bb + ((cc or not dd) xor aa) + X[12]+$5C4DD124; bb:= (t shl 7) or (t shr (32-7)); |
| 168 |
t:= aa + ((bb or not cc) xor dd) + X[ 4]+$5C4DD124; aa:= (t shl 6) or (t shr (32-6)); |
| 169 |
t:= dd + ((aa or not bb) xor cc) + X[ 9]+$5C4DD124; dd:= (t shl 15) or (t shr (32-15)); |
| 170 |
t:= cc + ((dd or not aa) xor bb) + X[ 1]+$5C4DD124; cc:= (t shl 13) or (t shr (32-13)); |
| 171 |
t:= bb + ((cc or not dd) xor aa) + X[ 2]+$5C4DD124; bb:= (t shl 11) or (t shr (32-11)); |
| 172 |
|
| 173 |
t:= aa + ((bb and cc) or (not bb and dd)) + X[15]+$6D703EF3; aa:= (t shl 9) or (t shr (32-9)); |
| 174 |
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 5]+$6D703EF3; dd:= (t shl 7) or (t shr (32-7)); |
| 175 |
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 1]+$6D703EF3; cc:= (t shl 15) or (t shr (32-15)); |
| 176 |
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 3]+$6D703EF3; bb:= (t shl 11) or (t shr (32-11)); |
| 177 |
t:= aa + ((bb and cc) or (not bb and dd)) + X[ 7]+$6D703EF3; aa:= (t shl 8) or (t shr (32-8)); |
| 178 |
t:= dd + ((aa and bb) or (not aa and cc)) + X[14]+$6D703EF3; dd:= (t shl 6) or (t shr (32-6)); |
| 179 |
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 6]+$6D703EF3; cc:= (t shl 6) or (t shr (32-6)); |
| 180 |
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 9]+$6D703EF3; bb:= (t shl 14) or (t shr (32-14)); |
| 181 |
t:= aa + ((bb and cc) or (not bb and dd)) + X[11]+$6D703EF3; aa:= (t shl 12) or (t shr (32-12)); |
| 182 |
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 8]+$6D703EF3; dd:= (t shl 13) or (t shr (32-13)); |
| 183 |
t:= cc + ((dd and aa) or (not dd and bb)) + X[12]+$6D703EF3; cc:= (t shl 5) or (t shr (32-5)); |
| 184 |
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 2]+$6D703EF3; bb:= (t shl 14) or (t shr (32-14)); |
| 185 |
t:= aa + ((bb and cc) or (not bb and dd)) + X[10]+$6D703EF3; aa:= (t shl 13) or (t shr (32-13)); |
| 186 |
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 0]+$6D703EF3; dd:= (t shl 13) or (t shr (32-13)); |
| 187 |
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 4]+$6D703EF3; cc:= (t shl 7) or (t shr (32-7)); |
| 188 |
t:= bb + ((cc and dd) or (not cc and aa)) + X[13]+$6D703EF3; bb:= (t shl 5) or (t shr (32-5)); |
| 189 |
|
| 190 |
t:= aa + (bb xor cc xor dd) + X[ 8]; aa:= (t shl 15) or (t shr (32-15)); |
| 191 |
t:= dd + (aa xor bb xor cc) + X[ 6]; dd:= (t shl 5) or (t shr (32-5)); |
| 192 |
t:= cc + (dd xor aa xor bb) + X[ 4]; cc:= (t shl 8) or (t shr (32-8)); |
| 193 |
t:= bb + (cc xor dd xor aa) + X[ 1]; bb:= (t shl 11) or (t shr (32-11)); |
| 194 |
t:= aa + (bb xor cc xor dd) + X[ 3]; aa:= (t shl 14) or (t shr (32-14)); |
| 195 |
t:= dd + (aa xor bb xor cc) + X[11]; dd:= (t shl 14) or (t shr (32-14)); |
| 196 |
t:= cc + (dd xor aa xor bb) + X[15]; cc:= (t shl 6) or (t shr (32-6)); |
| 197 |
t:= bb + (cc xor dd xor aa) + X[ 0]; bb:= (t shl 14) or (t shr (32-14)); |
| 198 |
t:= aa + (bb xor cc xor dd) + X[ 5]; aa:= (t shl 6) or (t shr (32-6)); |
| 199 |
t:= dd + (aa xor bb xor cc) + X[12]; dd:= (t shl 9) or (t shr (32-9)); |
| 200 |
t:= cc + (dd xor aa xor bb) + X[ 2]; cc:= (t shl 12) or (t shr (32-12)); |
| 201 |
t:= bb + (cc xor dd xor aa) + X[13]; bb:= (t shl 9) or (t shr (32-9)); |
| 202 |
t:= aa + (bb xor cc xor dd) + X[ 9]; aa:= (t shl 12) or (t shr (32-12)); |
| 203 |
t:= dd + (aa xor bb xor cc) + X[ 7]; dd:= (t shl 5) or (t shr (32-5)); |
| 204 |
t:= cc + (dd xor aa xor bb) + X[10]; cc:= (t shl 15) or (t shr (32-15)); |
| 205 |
t:= bb + (cc xor dd xor aa) + X[14]; bb:= (t shl 8) or (t shr (32-8)); |
| 206 |
|
| 207 |
Inc(dd,c + CurrentHash[1]); |
| 208 |
CurrentHash[1]:= CurrentHash[2] + d + aa; |
| 209 |
CurrentHash[2]:= CurrentHash[3] + a + bb; |
| 210 |
CurrentHash[3]:= CurrentHash[0] + b + cc; |
| 211 |
CurrentHash[0]:= dd; |
| 212 |
|
| 213 |
FillChar(X,Sizeof(X),0); |
| 214 |
Index:= 0; |
| 215 |
FillChar(HashBuffer,Sizeof(HashBuffer),0); |
| 216 |
end; |
| 217 |
|
| 218 |
class function TDCP_ripemd128.GetHashSize: integer; |
| 219 |
begin |
| 220 |
Result:= 128; |
| 221 |
end; |
| 222 |
|
| 223 |
class function TDCP_ripemd128.GetId: integer; |
| 224 |
begin |
| 225 |
Result:= DCP_ripemd128; |
| 226 |
end; |
| 227 |
|
| 228 |
class function TDCP_ripemd128.GetAlgorithm: string; |
| 229 |
begin |
| 230 |
Result:= 'RipeMD-128'; |
| 231 |
end; |
| 232 |
|
| 233 |
class function TDCP_ripemd128.SelfTest: boolean; |
| 234 |
const |
| 235 |
Test1Out: array[0..15] of byte= |
| 236 |
($86,$be,$7a,$fa,$33,$9d,$0f,$c7,$cf,$c7,$85,$e7,$2f,$57,$8d,$33); |
| 237 |
Test2Out: array[0..15] of byte= |
| 238 |
($fd,$2a,$a6,$07,$f7,$1d,$c8,$f5,$10,$71,$49,$22,$b3,$71,$83,$4e); |
| 239 |
var |
| 240 |
TestHash: TDCP_ripemd128; |
| 241 |
TestOut: array[0..15] of byte; |
| 242 |
begin |
| 243 |
dcpFillChar(TestOut, SizeOf(TestOut), 0); |
| 244 |
TestHash:= TDCP_ripemd128.Create(nil); |
| 245 |
TestHash.Init; |
| 246 |
TestHash.UpdateStr('a'); |
| 247 |
TestHash.Final(TestOut); |
| 248 |
Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)); |
| 249 |
TestHash.Init; |
| 250 |
TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz'); |
| 251 |
TestHash.Final(TestOut); |
| 252 |
Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result; |
| 253 |
TestHash.Free; |
| 254 |
end; |
| 255 |
|
| 256 |
procedure TDCP_ripemd128.Init; |
| 257 |
begin |
| 258 |
Burn; |
| 259 |
CurrentHash[0]:= $67452301; |
| 260 |
CurrentHash[1]:= $efcdab89; |
| 261 |
CurrentHash[2]:= $98badcfe; |
| 262 |
CurrentHash[3]:= $10325476; |
| 263 |
fInitialized:= true; |
| 264 |
end; |
| 265 |
|
| 266 |
procedure TDCP_ripemd128.Burn; |
| 267 |
begin |
| 268 |
LenHi:= 0; LenLo:= 0; |
| 269 |
Index:= 0; |
| 270 |
FillChar(HashBuffer,Sizeof(HashBuffer),0); |
| 271 |
FillChar(CurrentHash,Sizeof(CurrentHash),0); |
| 272 |
fInitialized:= false; |
| 273 |
end; |
| 274 |
|
| 275 |
procedure TDCP_ripemd128.Update(const Buffer; Size: longword); |
| 276 |
var |
| 277 |
PBuf: ^byte; |
| 278 |
begin |
| 279 |
if not fInitialized then |
| 280 |
raise EDCP_hash.Create('Hash not initialized'); |
| 281 |
|
| 282 |
Inc(LenHi,Size shr 29); |
| 283 |
Inc(LenLo,Size*8); |
| 284 |
if LenLo< (Size*8) then |
| 285 |
Inc(LenHi); |
| 286 |
|
| 287 |
PBuf:= @Buffer; |
| 288 |
while Size> 0 do |
| 289 |
begin |
| 290 |
if (Sizeof(HashBuffer)-Index)<= DWord(Size) then |
| 291 |
begin |
| 292 |
Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index); |
| 293 |
Dec(Size,Sizeof(HashBuffer)-Index); |
| 294 |
Inc(PBuf,Sizeof(HashBuffer)-Index); |
| 295 |
Compress; |
| 296 |
end |
| 297 |
else |
| 298 |
begin |
| 299 |
Move(PBuf^,HashBuffer[Index],Size); |
| 300 |
Inc(Index,Size); |
| 301 |
Size:= 0; |
| 302 |
end; |
| 303 |
end; |
| 304 |
end; |
| 305 |
|
| 306 |
procedure TDCP_ripemd128.Final(var Digest); |
| 307 |
begin |
| 308 |
if not fInitialized then |
| 309 |
raise EDCP_hash.Create('Hash not initialized'); |
| 310 |
HashBuffer[Index]:= $80; |
| 311 |
if Index>= 56 then |
| 312 |
Compress; |
| 313 |
PDWord(@HashBuffer[56])^:= LenLo; |
| 314 |
PDWord(@HashBuffer[60])^:= LenHi; |
| 315 |
Compress; |
| 316 |
Move(CurrentHash,Digest,Sizeof(CurrentHash)); |
| 317 |
Burn; |
| 318 |
end; |
| 319 |
|
| 320 |
|
| 321 |
end. |