I need to implement SHA512 within Jade.
SHA1 was simple and has been working well for years...
ExternalFunction
sha1(data: Binary; length: Integer; digest: Binary[20] output) is SHA1 in libeay32;
Any ideas for SHA-2 SHA512?
Hi concord,I need to implement SHA512 within Jade.
SHA1 was simple and has been working well for years...
ExternalFunction
sha1(data: Binary; length: Integer; digest: Binary[20] output) is SHA1 in libeay32;
Any ideas for SHA-2 SHA512?
Code: Select all
sha256(data: Binary; length: Integer64; digest: Binary[32] output) is SHA256 in libeay32;
Code: Select all
sha512(data: Binary; length: Integer64; digest: Binary[64] output) is "SHA512" in libeay32 applicationServerExecution;
Code: Select all
constants
BLOCK_SIZE = 64;
INNER_PAD = #[36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36];
OUTER_PAD = #[5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C];
vars
keyI, keyO : Binary;
bin1, bin2 : Binary;
keyAdj : Binary;
begin
if key.length > BLOCK_SIZE then
//--- key too big
keyAdj := key.Binary.sha1 (true);
keyI := INNER_PAD.bitXor (keyAdj);
keyO := OUTER_PAD.bitXor (keyAdj);
else
keyI := INNER_PAD.bitXor (key.Binary);
keyO := OUTER_PAD.bitXor (key.Binary);
endif;
bin1 := (keyI & data.Binary).sha1(true);
bin2 := (keyO & bin1).sha1 (true);
if raw_output then
return bin2.String;
else
return bin2.toHexDigits.toLower;
endif;
end;
Code: Select all
00000001 7BFA 95A6 8892 4C47 C7D2 2381 F20C C926 {ú•¦.’LGÇÒ#.ò.É&
00000017 F524 BEAC B13F 84E2 03D4 BD8C B6BA 2FCE õ$¾¬±?„â.Ô½Œ¶º/Î
00000033 81C5 7A5F 059B F3D5 0992 6487 BDE9 25B3 .Åz_.›óÕ.’d‡½é%³
00000049 BCEE 0635 E4F7 BAEB A054 E5DB A696 B2BF ¼î.5ä÷ºë TåÛ¦–²¿
Code: Select all
"JADE (C:\Concord_dev\system : Andrew : singleUser) - [CSBaseSche"
Code: Select all
vars
result : Binary[64];
begin
call sha512( "TEST".Binary, 4, result );
write result.display;
end;
Checking Wikidedia I see that SHA-224 and SHA-256 have a block size of 512 bits (64 bytes). SHA-384 and SHA-512 have a block size of 1024 bits (128 bytes). However, the block size of the HMAC function may be different. It will depend upon the block size of the HMAC algorithm, which may be tied to the hash size. I don't know what HMAC standard you are targetting. Whatever the block size, the padding is used to fill up to an entire block, as all data in the HMAC must be an exact multiple of the block size.Murray I asked a similar question back in 2012 and you totally nailed it for me then too!
I have one final hurdle, I'm almost certain the HMAC wrapper I have in Jade for sha1 was probably written by you back in 2012, I'm sure I need to tweak a new version of this for sha512, I assume the BLOCK_SIZE is greater... 128?
Code: Select all
constants BLOCK_SIZE = 64; INNER_PAD = #[36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36]; OUTER_PAD = #[5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C];
Code: Select all
vars
bin : Binary;
hash : Binary[64];
len : Integer64;
begin
hash := null;
bin := 'TEST'.Binary;
len := bin.length;
call sha512 (bin, len, hash);
write hash.display;
write hash.toHexDigits.String;
write 'Should be';
write '7bfa95a688924c47c7d22381f20cc926f524beacb13f84e203d4bd8cb6ba2fce81c57a5f059bf3d509926487bde925b3bcee0635e4f7baeba054e5dba696b2bf'.toUpper;
end;
Code: Select all
sha512(data: Binary; length: Integer64; digest: Binary[64] output) is "SHA512" in libeay32 presentationClientExecution;
Code: Select all
sha512(data: Binary; length: Integer64; digest: Binary[64] output) is "SHA512" in libeay32 applicationServerExecution;
Return to “Tips and Techniques”
Users browsing this forum: No registered users and 3 guests