B4J Question HMAC SHA256

DonManfred

Expert
Licensed User
Longtime User
I saw the SIGNATURE library
?? probably a library to draw a SIGNATURE on a Canvas and has nothing to do with HMAC SHA256

Which Library are you speaking of?
 
Upvote 0

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
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 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
Ho appena testato. Ho ottenuto gli stessi risultati di PHP.

[CODICE=php]<?php
echo hash_hmac('sha256', 'La veloce volpe marrone saltò sopra il cane pigro.', 'segreto');
?>[/CODICE]
Esempio tratto da https://www.php.net/manual/en/function.hash-hmac.php

B4X:
Dim results As String = HMACSHA256("La veloce volpe marrone saltò sopra il cane pigro.", "segreto")
Registro(risultati)

B4X:
Sub pubblico HMACSHA256 (str come stringa, chiave come stringa) come stringa
    Dim data() come byte
    Dim MC come Mac
    Dim KG come generatore di chiavi
    Dim BC come ByteConverter
  
    KG.Inizializza("HMACSHA256")
    KG.KeyFromBytes(chiave.GetBytes("UTF8"))
  
    MC.Inizializza("HMACSHA256", KG.Chiave)
    MC.Aggiornamento(str.GetBytes("UTF8"))
  
    dati = MC.Segno
    Restituisce BC.HexFromBytes(dati).ToLowerCase
Fine Sotto

Risultati: 9c5c42422b03f0ee32949920649445e417b2c634050833c5165704b825c2a53b
Thank you
 
Upvote 0
Top