B4J Question HMAC SHA256

LordZenzo

Well-Known Member
Licensed User
Longtime User
?? probably a library to draw a SIGNATURE on a Canvas and has nothing to do with HMAC SHA256

Which Library are you speaking of?
HI
sorry, I was stuck trying
signature is part of the Encription library
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You will need ByteConverter (or StringUtils) and Encryption libraries.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
The last thread is a good example.

 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
I tried to implement the solution but I'm not sure it works, let me explain
using PHP I have
$hmac = hash_hmac('sha256', $message, $secretKey);
which returns a value
using the suggested code I get another value, I don't think PHP is wrong
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think the value will be different every time it is executed. Does PHP return same value every time?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I just tested. I got the same results as PHP.

PHP:
<?php
echo hash_hmac('sha256', 'The quick brown fox jumped over the lazy dog.', 'secret');
?>
Example taken from https://www.php.net/manual/en/function.hash-hmac.php

B4X:
Dim results As String = HMACSHA256("The quick brown fox jumped over the lazy dog.", "secret")
Log(results)

B4X:
Public Sub HMACSHA256 (str As String, key As String) As String
    Dim data() As Byte
    Dim MC As Mac
    Dim KG As KeyGenerator
    Dim BC As ByteConverter
    
    KG.Initialize("HMACSHA256")
    KG.KeyFromBytes(key.GetBytes("UTF8"))
    
    MC.Initialise("HMACSHA256", KG.Key)
    MC.Update(str.GetBytes("UTF8"))
    
    data = MC.Sign
    Return BC.HexFromBytes(data).ToLowerCase
End Sub

Results: 9c5c42422b03f0ee32949920649445e417b2c634050833c5165704b825c2a53b
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
Thank you
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…