Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { BinData, JsonObject, Jws } from '@chilkat/react-native'

function chilkatExample() {
  const jws = new Jws();

  // Protected header specifying HMAC-SHA256.
  const header = new JsonObject();
  header.updateString('alg', 'HS256');
  try {
    jws.setProtectedHeader(0, header);
  } catch {
    console.log(jws.lastErrorText);
    return;
  }

  const bIncludeBom = false;
  try {
    jws.setPayload('This is the content to sign.', 'utf-8', bIncludeBom);
  } catch {
    console.log(jws.lastErrorText);
    return;
  }

  // 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.
  const bdKey = new BinData();
  bdKey.appendEncoded('YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=', 'base64');
  try {
    jws.setMacKeyBd(0, bdKey);
  } catch {
    console.log(jws.lastErrorText);
    return;
  }

  let jwsCompact: string;
  try {
    jwsCompact = jws.createJws();
  } catch {
    console.log(jws.lastErrorText);
    return;
  }

  console.log(jwsCompact);
}