sign method

  1. @override
Uint8List sign(
  1. JWTKey key,
  2. Uint8List body
)
override

Create a signature of the body with key

return the signature as bytes

Implementation

@override
Uint8List sign(JWTKey key, Uint8List body) {
  assert(key is ECPrivateKey, 'key must be a ECPrivateKey');
  final privateKey = key as ECPrivateKey;

  final signer = pc.Signer('${_getHash(name)}/DET-ECDSA');
  final params = pc.PrivateKeyParameter<pc.ECPrivateKey>(privateKey.key);

  signer.init(true, params);

  final signature = signer.generateSignature(
    Uint8List.fromList(body),
  ) as pc.ECSignature;

  final rBytes = bigIntToBytes(signature.r).toList();
  while (rBytes.length < 32) {
    rBytes.add(0);
  }

  final sBytes = bigIntToBytes(signature.s).toList();
  while (sBytes.length < 32) {
    sBytes.add(0);
  }

  final len = privateKey.size;
  final bytes = Uint8List(len * 2);

  bytes.setRange(len - rBytes.length, len, rBytes.reversed);
  bytes.setRange((len * 2) - sBytes.length, len * 2, sBytes.reversed);

  return bytes;
}