Alright, I'm working on some hackthissite mission which requires you to write a Sha1 hash function..
So I made one in scar! Have phun <3:
SCAR Code:
program Sha1;
Const
Str = 'test'; //The string you wanne test..
function SaveInt64InStr( int : int64) : string;
var
I : integer;
begin;
SetLengtH(result,8);
for i := 7 downto 0 do
Result[8-I] := chr(int shr (i*8) and $FF);
end;
function StrToIn64(Str : string) : Int64;
var
I : integer;
begin;
for i := 0 to 7 do
Result := Result or (ord(str[8-i]) shl (i*8));
end;
Function String2LongWord(Str : string) : LongWord;
var
I : integer;
begin;
for i := 3 downto 0 do
Result := Result or (ord(str[4-I]) shl (i*8));
end;
Function LongWord2String(LWord : LongWord) : string;
var
I : integer;
begin;
SetLength(result,4);
for i := 3 downto 0 do
Result[4-I] := chr(LWord shr (i*8) and $FF);
end;
function CircularShift(bits : integer; word : longword) : longword;
begin;
result := (word shl bits) or (word shr (32-bits));
end;
function IntToBin ( value: integer; digits: integer ): string;
begin
result := StringOfChar ( '0', digits ) ;
while value > 0 do begin
if ( value and 1 ) = 1 then
result [ digits ] := '1';
dec ( digits ) ;
value := value shr 1;
end;
end;
function ByteToHex(ThaByte : byte) : string;
var
Hexx : string;
begin;
Hexx := '0123456789ABCDEF';
SetLength(result,2);
Result[1] := Hexx[(ThaByte div 16) mod 16 + 1];
Result[2] := Hexx[ThaByte mod 16 + 1];
end;
Function Sha1(Str : string; ReturnInHex : boolean): String;
var
L : Int64;
Block : string;
I : integer;
A,B,C,D,E,F, Temp: LongWord;
H,KCodes: Array of LongWord;
W : Array[0..79] of LongWord;
begin;
h := [$67452301,$EFCDAB89,$98BADCFE,$10325476,$C3D2E1F0];
KCodes := [$5A827999,$6ED9EBA1,$8F1BBCDC,$CA62C1D6];
Result := str;
L := Int64(Length(str))*8;
Result := result + chr($80);
while (Length(result) mod 64) <> 56 do
Result := Result + chr(0);
Result := result + SaveInt64InStr(L);
while Length(result) > 0 do
begin;
block := Copy(result,1,64);
Delete(result,1,64);
for i := 0 to 15 do
W[i] := String2LongWord(Copy(block,i*4+1,4));
for i := 16 to 79 do
w[i] := CircularShift(1,(w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]));
a := h[0];
b := h[1];
c := h[2];
d := h[3];
e := h[4];
for i := 0 to 79 do
begin;
if i < 20 then
f := (b and c) or ((not b) and d)
else if i < 40 then
f := b xor c xor d
else if i < 60 then
f := (b and c) or (b and d) or (c and d)
else if i < 80 then
f := b xor c xor d;
temp := CircularShift(5,a) + f + e + KCodes[i div 20] + w[i];
e := d
d := c
c := CircularShift(30,b);
b := a
a := temp
end;
h[0] := h[0] + a;
h[1] := h[1] + b;
h[2] := h[2] + c;
h[3] := h[3] + d;
h[4] := h[4] + e;
end;
Result := '';
if not ReturnInHex then
for i := 0 to 4 do
Result := Result + LongWord2String(h[i])
else
for i := 0 to 19 do
Result := result + ByteToHex(Byte(H[i shr 2] shr (8 * ((3-i) mod 4)) and $ff))
end;
function Sha1Working : boolean;
begin;
result := (Sha1('test',true) = 'A94A8FE5CCB19BA61C4C0873D391E987982FBBD3');
end;
var
Hash,Binary : string;
I : integer;
begin
Writeln('Sha1Selftest. Sha1Working returned: ' + booltostr(Sha1Working));
Writeln('Hex:' + Sha1(Str,true));
Hash := sha1(str,false);
Writeln('Normal: ' + Hash);
for i := 1 to 20 do
Binary := Binary + IntToBin(ord(hash[i]),8);
Writeln('Binary: ' + Binary);
end.
Source:
http://en.wikipedia.org/wiki/SHA-1
http://www.faqs.org/rfcs/rfc3174.html
And I stole IntToBin from about.com ^^