toJWK method
Convert the key to a JWK JSON object representation
Implementation
@override
Map<String, dynamic> toJWK({String? keyID, RSAAlgorithm? algorithm}) {
final p = key.p;
if (p == null) throw ArgumentError('p is null');
final q = key.q;
if (q == null) throw ArgumentError('q is null');
final n = key.n;
if (n == null) throw ArgumentError('n is null');
final e = key.publicExponent;
if (e == null) throw ArgumentError('e is null');
final d = key.privateExponent;
if (d == null) throw ArgumentError('d is null');
final dp = d % (p - BigInt.one);
final dq = d % (q - BigInt.one);
final qi = q.modInverse(p);
Map<String, dynamic> jwk = {
'kty': 'RSA',
'use': 'sig',
'p': base64Unpadded(base64Url.encode(bigIntToBytes(p).reversed.toList())),
'q': base64Unpadded(base64Url.encode(bigIntToBytes(q).reversed.toList())),
'd': base64Unpadded(base64Url.encode(bigIntToBytes(d).reversed.toList())),
'e': base64Unpadded(base64Url.encode(bigIntToBytes(e).reversed.toList())),
'dp':
base64Unpadded(base64Url.encode(bigIntToBytes(dp).reversed.toList())),
'dq':
base64Unpadded(base64Url.encode(bigIntToBytes(dq).reversed.toList())),
'qi':
base64Unpadded(base64Url.encode(bigIntToBytes(qi).reversed.toList())),
'n': base64Unpadded(base64Url.encode(bigIntToBytes(n).reversed.toList())),
};
if (keyID != null) jwk['kid'] = keyID;
if (algorithm != null) jwk['alg'] = algorithm.name;
return jwk;
}