#1 2025-02-06 11:36:55

g_garzotto
Member
Registered: 2023-09-07
Posts: 4

Different behaviour of HmacSha1 between Delphi and Postman CryptoJS

Hello,

I'm using Delphi 7 and mORMot2.

This code:

procedure TForm1.btnGeneraCodiceClick(Sender: TObject);
const
  SECRET_KEY: RawByteString = 'myPrivateKey12345!!';
var
  sData,
  sSignature64: RawByteString;

  SHAOutput: TSHA1Digest;
begin
  sData := 'POS-1|2025-01-25|43';
  HmacSha1(SECRET_KEY, sData, SHAOutput);
  sSignature64 := BinToBase64URI(@SHAOutput, 20);
end;

returns in variable sSignature64 the value

AwxPbTUf5OGaFaxmmV6HwgpA0wU

which have a lenght of 27 chars.

The following code executed in Postman:

var CryptoJS = require('crypto-js');

var secretKey = "myPrivateKey12345!!";
var dataString = "POS-1|2025-01-25|43";

var signatureBytes = CryptoJS.HmacSHA1(dataString, secretKey);
var signatureBase64 = CryptoJS.enc.Base64.stringify(signatureBytes);
console.log("signatureBase64: ", signatureBase64);

returns in variable signatureBase64 the value

AwxPbTUf5OGaFaxmmV6HwgpA0wU=

which have a lenght of 28 chars.

I can't figure out if I wrong something in my code, or if there are some issues in the code.

I found the same behaviour also in mORMot 1.

Can you point me in some directions to solve my problem ?

Thanks in advice, regards

Giuseppe Garzotto

Offline

#2 2025-02-06 19:08:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,970
Website

Re: Different behaviour of HmacSha1 between Delphi and Postman CryptoJS

BinToBase64URI() returns a base64-URI encoded value, not base64.

Offline

#3 2025-02-07 07:46:16

g_garzotto
Member
Registered: 2023-09-07
Posts: 4

Re: Different behaviour of HmacSha1 between Delphi and Postman CryptoJS

Didn't notice the URI, you're right.

The following code works fine:

procedure TForm1.btnGeneraCodiceClick(Sender: TObject);
const
  SECRET_KEY: RawByteString = 'myPrivateKey12345!!';
var
  sData,
  sSignature64: RawByteString;

  SHAOutput: TSHA1Digest;
begin
  sData := 'POS-1|2025-01-25|43';
  HmacSha1(SECRET_KEY, sData, SHAOutput);
  sSignature64 := BinToBase64(@SHAOutput, 20);
end;

Thanks

Offline

Board footer

Powered by FluxBB