Sample code for 30+ languages & platforms
Delphi DLL

Sign a JWS with an HMAC Key from BinData

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.

Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
jws: HCkJws;
header: HCkJsonObject;
bIncludeBom: Boolean;
bdKey: HCkBinData;
jwsCompact: PWideChar;

begin
success := False;

jws := CkJws_Create();

//  Protected header specifying HMAC-SHA256.
header := CkJsonObject_Create();
CkJsonObject_UpdateString(header,'alg','HS256');
success := CkJws_SetProtectedHeader(jws,0,header);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;

bIncludeBom := False;
success := CkJws_SetPayload(jws,'This is the content to sign.','utf-8',bIncludeBom);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;

//  Set the symmetric MAC key for signature 0 from the raw bytes in a BinData.  In production, obtain
//  the key material from a secure source.
bdKey := CkBinData_Create();
CkBinData_AppendEncoded(bdKey,'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=','base64');
success := CkJws_SetMacKeyBd(jws,0,bdKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;

jwsCompact := CkJws__createJws(jws);
if (CkJws_getLastMethodSuccess(jws) = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;
Memo1.Lines.Add(jwsCompact);

CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkBinData_Dispose(bdKey);