fork download
  1. program Prog;
  2.  
  3. uses Classes, SysUtils;
  4.  
  5. function leftrotate(x, c: Cardinal): Cardinal;
  6. begin
  7. leftrotate := (x shl c) or (x shr (32-c));
  8. end;
  9.  
  10. const s: array[0..63] of Cardinal = (
  11. 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  12. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  13. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  14. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 );
  15. K: array[0..63] of Cardinal = (
  16. $d76aa478, $e8c7b756, $242070db, $c1bdceee,
  17. $f57c0faf, $4787c62a, $a8304613, $fd469501,
  18. $698098d8, $8b44f7af, $ffff5bb1, $895cd7be,
  19. $6b901122, $fd987193, $a679438e, $49b40821,
  20. $f61e2562, $c040b340, $265e5a51, $e9b6c7aa,
  21. $d62f105d, $02441453, $d8a1e681, $e7d3fbc8,
  22. $21e1cde6, $c33707d6, $f4d50d87, $455a14ed,
  23. $a9e3e905, $fcefa3f8, $676f02d9, $8d2a4c8a,
  24. $fffa3942, $8771f681, $6d9d6122, $fde5380c,
  25. $a4beea44, $4bdecfa9, $f6bb4b60, $bebfbc70,
  26. $289b7ec6, $eaa127fa, $d4ef3085, $04881d05,
  27. $d9d4d039, $e6db99e5, $1fa27cf8, $c4ac5665,
  28. $f4292244, $432aff97, $ab9423a7, $fc93a039,
  29. $655b59c3, $8f0ccc92, $ffeff47d, $85845dd1,
  30. $6fa87e4f, $fe2ce6e0, $a3014314, $4e0811a1,
  31. $f7537e82, $bd3af235, $2ad7d2bb, $eb86d391 );
  32.  
  33. var a0,b0,c0,d0, a,b,c,d, f,g,dTemp: Cardinal;
  34. Len: Integer;
  35. Msg: array[0..63] of Char;
  36. M: array[0..15] of Cardinal absolute Msg; //break chunk into sixteen 32-bit words M[j]
  37. Str: String;
  38. i: Integer;
  39. ff: TFileStream;
  40. wait: Char;
  41. begin
  42. a0 := $67452301;
  43. b0 := $efcdab89;
  44. c0 := $98badcfe;
  45. d0 := $10325476;
  46.  
  47. Str := 'hello world';
  48. Len := Length(Str);
  49.  
  50. FillChar(Msg, 64, 0);
  51.  
  52. for i:=1 to Len do Msg[i-1] := Str[i];
  53.  
  54. //append "1" bit to message
  55. Msg[Len] := chr(128);
  56.  
  57. //append original length in bits mod (2 pow 64) to message
  58. Msg[63-7] := chr(8*Len); //Update thanks to @MBo
  59.  
  60. //Process each 512-bit chunk of message- 1 only have 1 chunk
  61.  
  62. //TEST dump
  63. // ff := TFileStream.create('test.txt', fmCreate);
  64. // ff.write(msg, 64);
  65. // ff.free;
  66.  
  67. //Initialize hash value for this chunk:
  68. A := a0;
  69. B := b0;
  70. C := c0;
  71. D := d0;
  72.  
  73. //Main loop:
  74. for i := 0 to 63 do begin
  75.  
  76. if (i>=0) and (i<=15) then begin
  77. F := (B and C) or ((not B) and D);
  78. g := i;
  79. end
  80. else if (i>=16) and (i<=31) then begin
  81. F := (D and B) or ((not D) and C);
  82. g := (5*i + 1) mod 16;
  83. end
  84. else if (i>=32) and (i<=47) then begin
  85. F := B xor C xor D;
  86. g := (3*i + 5) mod 16;
  87. end
  88. else if (i>=48) and (i<=63) then begin
  89. F := C xor (B or (not D));
  90. g := (7*i) mod 16;
  91. end;
  92.  
  93. dTemp := D;
  94. D := C;
  95. C := B;
  96. B := B + leftrotate((A + F + K[i] + M[g]), s[i]);
  97. A := dTemp;
  98. end;
  99.  
  100. //Add this chunk's hash to result so far:
  101. a0 := a0 + A;
  102. b0 := b0 + B;
  103. c0 := c0 + C;
  104. d0 := d0 + D;
  105.  
  106. //This should give 5EB63BBBE01EEED093CB22BB8F5ACDC3
  107. Writeln( IntToHex(a0,8) + IntToHex(b0,8) + IntToHex(c0,8) +IntToHex(d0,8) );
  108.  
  109. Readln(wait);
  110. end.
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.pas:3: error: module/unit interface `Classes' could not be imported
stdout
Standard output is empty