Results 1 to 11 of 11

Thread: Sha1, hash function.

  1. #1
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default Sha1, hash function.

    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 ^^
    Verrekte Koekwous

  2. #2
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Sexy. Now I can go do some HackThisSite without any work

  3. #3
    Join Date
    Dec 2006
    Location
    Sweden
    Posts
    10,812
    Mentioned
    3 Post(s)
    Quoted
    16 Post(s)

    Default

    You are epic.


    Send SMS messages using Simba
    Please do not send me a PM asking for help; I will not be able to help you! Post in a relevant thread or make your own! And always remember to search first!

  4. #4
    Join Date
    Mar 2006
    Posts
    13,241
    Mentioned
    228 Post(s)
    Quoted
    267 Post(s)

    Default

    Woot less work for me on HTS
    Thanks
    STOP PM'ING ME

  5. #5
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Hobbit View Post
    Woot less work for me on HTS
    Thanks
    It's actually a PART of the mission ^^. I'm working on the other part right now, blowfish decryption
    Verrekte Koekwous

  6. #6
    Join Date
    Apr 2006
    Location
    I live in NH
    Posts
    611
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Cool! I've looked at those wikipedia pages so many times trying to recreate the functions in scar, but I always failed. The types always seemed to confuse me. Nice work!

  7. #7
    Join Date
    Apr 2008
    Location
    Northwest england
    Posts
    1,179
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Ron hmm aint seen you around guessing your inactive come back =] HTS pwns
    Blank!

  8. #8
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Ron View Post
    Cool! I've looked at those wikipedia pages so many times trying to recreate the functions in scar, but I always failed. The types always seemed to confuse me. Nice work!
    Confused me a lot to, Big-Endian \ Little-Endian -> Int64, DWord.. All weird stuff
    Verrekte Koekwous

  9. #9
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I almost phailed at the first mission of HTS
    You want to know why? I was thinking the hard way
    ~Hermen

  10. #10
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    Big-Endian: You get the big end first, so $F0, byte one would be F. Other way for Little-Endian.

    They should use one, having two ways fails.
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  11. #11
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by R0b0t1 View Post
    Big-Endian: You get the big end first, so $F0, byte one would be F. Other way for Little-Endian.

    They should use one, having two ways fails.
    Yep It's proper annoying.
    Verrekte Koekwous

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 02-27-2008, 05:20 PM
  2. Replies: 2
    Last Post: 02-26-2008, 08:26 PM
  3. [FUNCTION] FindDoorColour: integer; By ZephyrsFury [FUNCTION]
    By ZephyrsFury in forum Research & Development Lounge
    Replies: 10
    Last Post: 07-27-2007, 08:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •