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 RSAPrivateKey, 'key must be a RSAPrivateKey');
  final privateKey = key as RSAPrivateKey;

  final algorithm = _getAlgorithm(name);

  final signer = pc.Signer('${_getHash(name)}/${algorithm}');
  pc.CipherParameters params = pc.PrivateKeyParameter<pc.RSAPrivateKey>(
    privateKey.key,
  );

  if (algorithm == 'PSS') {
    final random = _random ?? Random.secure();
    final salt = Uint8List.fromList(
      List.generate(32, (_) => random.nextInt(256)),
    );

    params = pc.ParametersWithSalt(
      params,
      salt,
    );
  }

  signer.init(true, params);

  final signature = signer.generateSignature(Uint8List.fromList(body));

  if (signature is pc.PSSSignature) {
    return signature.bytes;
  } else {
    return (signature as pc.RSASignature).bytes;
  }
}