toJWK method

  1. @override
Map<String, dynamic> toJWK({
  1. String? keyID,
  2. ECDSAAlgorithm? algorithm,
})
override

Convert the key to a JWK JSON object representation

Implementation

@override
Map<String, dynamic> toJWK({String? keyID, ECDSAAlgorithm? algorithm}) {
  final params = key.parameters;
  if (params == null) throw ArgumentError('parameters is null');
  final curve = curveOpenSSLToNIST(params.domainName);
  final x = key.Q?.x?.toBigInteger();
  if (x == null) throw ArgumentError('x is null');
  final y = key.Q?.y?.toBigInteger();
  if (y == null) throw ArgumentError('y is null');

  Map<String, dynamic> jwk = {
    'kty': 'EC',
    'use': 'sig',
    'crv': curve,
    'x': base64Unpadded(base64Url.encode(bigIntToBytes(x).reversed.toList())),
    'y': base64Unpadded(base64Url.encode(bigIntToBytes(y).reversed.toList())),
  };

  if (keyID != null) jwk['kid'] = keyID;
  final alg = algorithm?.name ?? ecCurveToAlgorithm(curve)?.name;
  if (alg != null) jwk['alg'] = alg;

  return jwk;
}