with Ada.Unchecked_Conversion;
package body GNAT.MD5 is
use Interfaces;
Padding : constant String :=
(1 => Character'Val (16#80#), 2 .. 64 => ASCII.NUL);
Hex_Digit : constant array (Unsigned_32 range 0 .. 15) of Character :=
('0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
S11 : constant := 7;
S12 : constant := 12;
S13 : constant := 17;
S14 : constant := 22;
S21 : constant := 5;
S22 : constant := 9;
S23 : constant := 14;
S24 : constant := 20;
S31 : constant := 4;
S32 : constant := 11;
S33 : constant := 16;
S34 : constant := 23;
S41 : constant := 6;
S42 : constant := 10;
S43 : constant := 15;
S44 : constant := 21;
type Sixteen_Words is array (Natural range 0 .. 15)
of Interfaces.Unsigned_32;
procedure Decode
(Block : String;
X : out Sixteen_Words);
function F (X, Y, Z : Unsigned_32) return Unsigned_32;
pragma Inline (F);
procedure FF
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive);
pragma Inline (FF);
function G (X, Y, Z : Unsigned_32) return Unsigned_32;
pragma Inline (G);
procedure GG
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive);
pragma Inline (GG);
function H (X, Y, Z : Unsigned_32) return Unsigned_32;
pragma Inline (H);
procedure HH
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive);
pragma Inline (HH);
function I (X, Y, Z : Unsigned_32) return Unsigned_32;
pragma Inline (I);
procedure II
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive);
pragma Inline (II);
procedure Transform
(C : in out Context;
Block : String);
procedure Decode
(Block : String;
X : out Sixteen_Words)
is
Cur : Positive := Block'First;
begin
pragma Assert (Block'Length = 64);
for Index in X'Range loop
X (Index) :=
Unsigned_32 (Character'Pos (Block (Cur))) +
Shift_Left (Unsigned_32 (Character'Pos (Block (Cur + 1))), 8) +
Shift_Left (Unsigned_32 (Character'Pos (Block (Cur + 2))), 16) +
Shift_Left (Unsigned_32 (Character'Pos (Block (Cur + 3))), 24);
Cur := Cur + 4;
end loop;
end Decode;
function Digest (C : Context) return Message_Digest is
Result : Message_Digest;
Cur : Natural := 1;
procedure Convert (X : Unsigned_32);
procedure Convert (X : Unsigned_32) is
Y : Unsigned_32 := X;
begin
for J in 1 .. 4 loop
Result (Cur + 1) := Hex_Digit (Y and Unsigned_32'(16#0F#));
Y := Shift_Right (Y, 4);
Result (Cur) := Hex_Digit (Y and Unsigned_32'(16#0F#));
Y := Shift_Right (Y, 4);
Cur := Cur + 2;
end loop;
end Convert;
begin
Convert (C.A);
Convert (C.B);
Convert (C.C);
Convert (C.D);
return Result;
end Digest;
function Digest (S : String) return Message_Digest is
C : Context;
begin
Update (C, S);
return Digest (C);
end Digest;
function Digest
(A : Ada.Streams.Stream_Element_Array)
return Message_Digest
is
C : Context;
begin
Update (C, A);
return Digest (C);
end Digest;
function F (X, Y, Z : Unsigned_32) return Unsigned_32 is
begin
return (X and Y) or ((not X) and Z);
end F;
procedure FF
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive)
is
begin
A := A + F (B, C, D) + X + AC;
A := Rotate_Left (A, S);
A := A + B;
end FF;
function G (X, Y, Z : Unsigned_32) return Unsigned_32 is
begin
return (X and Z) or (Y and (not Z));
end G;
procedure GG
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive)
is
begin
A := A + G (B, C, D) + X + AC;
A := Rotate_Left (A, S);
A := A + B;
end GG;
function H (X, Y, Z : Unsigned_32) return Unsigned_32 is
begin
return X xor Y xor Z;
end H;
procedure HH
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive)
is
begin
A := A + H (B, C, D) + X + AC;
A := Rotate_Left (A, S);
A := A + B;
end HH;
function I (X, Y, Z : Unsigned_32) return Unsigned_32 is
begin
return Y xor (X or (not Z));
end I;
procedure II
(A : in out Unsigned_32;
B, C, D : Unsigned_32;
X : Unsigned_32;
AC : Unsigned_32;
S : Positive)
is
begin
A := A + I (B, C, D) + X + AC;
A := Rotate_Left (A, S);
A := A + B;
end II;
procedure Transform
(C : in out Context;
Block : String)
is
X : Sixteen_Words;
AA : Unsigned_32 := C.A;
BB : Unsigned_32 := C.B;
CC : Unsigned_32 := C.C;
DD : Unsigned_32 := C.D;
begin
pragma Assert (Block'Length = 64);
Decode (Block, X);
FF (AA, BB, CC, DD, X (00), 16#D76aa478#, S11); FF (DD, AA, BB, CC, X (01), 16#E8c7b756#, S12); FF (CC, DD, AA, BB, X (02), 16#242070db#, S13); FF (BB, CC, DD, AA, X (03), 16#C1bdceee#, S14);
FF (AA, BB, CC, DD, X (04), 16#f57c0faf#, S11); FF (DD, AA, BB, CC, X (05), 16#4787c62a#, S12); FF (CC, DD, AA, BB, X (06), 16#a8304613#, S13); FF (BB, CC, DD, AA, X (07), 16#fd469501#, S14);
FF (AA, BB, CC, DD, X (08), 16#698098d8#, S11); FF (DD, AA, BB, CC, X (09), 16#8b44f7af#, S12); FF (CC, DD, AA, BB, X (10), 16#ffff5bb1#, S13); FF (BB, CC, DD, AA, X (11), 16#895cd7be#, S14);
FF (AA, BB, CC, DD, X (12), 16#6b901122#, S11); FF (DD, AA, BB, CC, X (13), 16#fd987193#, S12); FF (CC, DD, AA, BB, X (14), 16#a679438e#, S13); FF (BB, CC, DD, AA, X (15), 16#49b40821#, S14);
GG (AA, BB, CC, DD, X (01), 16#f61e2562#, S21); GG (DD, AA, BB, CC, X (06), 16#c040b340#, S22); GG (CC, DD, AA, BB, X (11), 16#265e5a51#, S23); GG (BB, CC, DD, AA, X (00), 16#e9b6c7aa#, S24);
GG (AA, BB, CC, DD, X (05), 16#d62f105d#, S21); GG (DD, AA, BB, CC, X (10), 16#02441453#, S22); GG (CC, DD, AA, BB, X (15), 16#d8a1e681#, S23); GG (BB, CC, DD, AA, X (04), 16#e7d3fbc8#, S24);
GG (AA, BB, CC, DD, X (09), 16#21e1cde6#, S21); GG (DD, AA, BB, CC, X (14), 16#c33707d6#, S22); GG (CC, DD, AA, BB, X (03), 16#f4d50d87#, S23); GG (BB, CC, DD, AA, X (08), 16#455a14ed#, S24);
GG (AA, BB, CC, DD, X (13), 16#a9e3e905#, S21); GG (DD, AA, BB, CC, X (02), 16#fcefa3f8#, S22); GG (CC, DD, AA, BB, X (07), 16#676f02d9#, S23); GG (BB, CC, DD, AA, X (12), 16#8d2a4c8a#, S24);
HH (AA, BB, CC, DD, X (05), 16#fffa3942#, S31); HH (DD, AA, BB, CC, X (08), 16#8771f681#, S32); HH (CC, DD, AA, BB, X (11), 16#6d9d6122#, S33); HH (BB, CC, DD, AA, X (14), 16#fde5380c#, S34);
HH (AA, BB, CC, DD, X (01), 16#a4beea44#, S31); HH (DD, AA, BB, CC, X (04), 16#4bdecfa9#, S32); HH (CC, DD, AA, BB, X (07), 16#f6bb4b60#, S33); HH (BB, CC, DD, AA, X (10), 16#bebfbc70#, S34);
HH (AA, BB, CC, DD, X (13), 16#289b7ec6#, S31); HH (DD, AA, BB, CC, X (00), 16#eaa127fa#, S32); HH (CC, DD, AA, BB, X (03), 16#d4ef3085#, S33); HH (BB, CC, DD, AA, X (06), 16#04881d05#, S34);
HH (AA, BB, CC, DD, X (09), 16#d9d4d039#, S31); HH (DD, AA, BB, CC, X (12), 16#e6db99e5#, S32); HH (CC, DD, AA, BB, X (15), 16#1fa27cf8#, S33); HH (BB, CC, DD, AA, X (02), 16#c4ac5665#, S34);
II (AA, BB, CC, DD, X (00), 16#f4292244#, S41); II (DD, AA, BB, CC, X (07), 16#432aff97#, S42); II (CC, DD, AA, BB, X (14), 16#ab9423a7#, S43); II (BB, CC, DD, AA, X (05), 16#fc93a039#, S44);
II (AA, BB, CC, DD, X (12), 16#655b59c3#, S41); II (DD, AA, BB, CC, X (03), 16#8f0ccc92#, S42); II (CC, DD, AA, BB, X (10), 16#ffeff47d#, S43); II (BB, CC, DD, AA, X (01), 16#85845dd1#, S44);
II (AA, BB, CC, DD, X (08), 16#6fa87e4f#, S41); II (DD, AA, BB, CC, X (15), 16#fe2ce6e0#, S42); II (CC, DD, AA, BB, X (06), 16#a3014314#, S43); II (BB, CC, DD, AA, X (13), 16#4e0811a1#, S44);
II (AA, BB, CC, DD, X (04), 16#f7537e82#, S41); II (DD, AA, BB, CC, X (11), 16#bd3af235#, S42); II (CC, DD, AA, BB, X (02), 16#2ad7d2bb#, S43); II (BB, CC, DD, AA, X (09), 16#eb86d391#, S44);
C.A := C.A + AA;
C.B := C.B + BB;
C.C := C.C + CC;
C.D := C.D + DD;
end Transform;
procedure Update
(C : in out Context;
Input : String)
is
Cur : Positive := Input'First;
Last_Block : String (1 .. 64);
begin
while Cur + 63 <= Input'Last loop
Transform (C, Input (Cur .. Cur + 63));
Cur := Cur + 64;
end loop;
Last_Block (1 .. Input'Last - Cur + 1) := Input (Cur .. Input'Last);
if Input'Last - Cur + 1 > 56 then
Cur := Input'Last - Cur + 2;
Last_Block (Cur .. 64) := Padding (1 .. 64 - Cur + 1);
Transform (C, Last_Block);
Last_Block := (others => ASCII.NUL);
else
Cur := Input'Last - Cur + 2;
Last_Block (Cur .. 56) := Padding (1 .. 56 - Cur + 1);
end if;
Last_Block (57 .. 64) := (others => ASCII.NUL);
declare
L : Unsigned_64 := Unsigned_64 (Input'Length) * 8;
begin
Cur := 57;
while L > 0 loop
Last_Block (Cur) := Character'Val (L and 16#Ff#);
L := Shift_Right (L, 8);
Cur := Cur + 1;
end loop;
end;
Transform (C, Last_Block);
end Update;
procedure Update
(C : in out Context;
Input : Ada.Streams.Stream_Element_Array)
is
subtype Stream_Array is Ada.Streams.Stream_Element_Array (Input'Range);
subtype Stream_String is
String (1 + Integer (Input'First) .. 1 + Integer (Input'Last));
function To_String is new Ada.Unchecked_Conversion
(Stream_Array, Stream_String);
String_Input : constant String := To_String (Input);
begin
Update (C, String_Input);
end Update;
function Wide_Digest (W : Wide_String) return Message_Digest is
C : Context;
begin
Wide_Update (C, W);
return Digest (C);
end Wide_Digest;
procedure Wide_Update
(C : in out Context;
Input : Wide_String)
is
String_Input : String (1 .. 2 * Input'Length);
Cur : Positive := 1;
begin
for Index in Input'Range loop
String_Input (Cur) :=
Character'Val
(Unsigned_32 (Wide_Character'Pos (Input (Index))) and 16#FF#);
Cur := Cur + 1;
String_Input (Cur) :=
Character'Val
(Shift_Right (Unsigned_32 (Wide_Character'Pos (Input (Index))), 8)
and 16#FF#);
Cur := Cur + 1;
end loop;
Update (C, String_Input);
end Wide_Update;
end GNAT.MD5;