Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final jws = CkJws();

  // Protected header specifying HMAC-SHA256.
  final header = CkJsonObject();
  header.updateString('alg', 'HS256');
  try {
    jws.setProtectedHeader(0, header);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final bIncludeBom = false;
  try {
    jws.setPayload('This is the content to sign.', 'utf-8', bIncludeBom);
  } on ChilkatException catch (e) {
    print(e.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.
  final bdKey = CkBinData();
  bdKey.appendEncoded('YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=', 'base64');
  try {
    jws.setMacKeyBd(0, bdKey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final String jwsCompact;
  try {
    jwsCompact = jws.createJws();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print(jwsCompact);
}