fromJwk static method
Create a key pair from a JsonWebKey
Implementation
static KeyPair? fromJwk(Map<String, dynamic> jwk) {
switch (jwk['kty']) {
case 'oct':
var key = SymmetricKey(keyValue: _base64ToBytes(jwk['k']) as Uint8List);
return KeyPair(publicKey: key, privateKey: key);
case 'RSA':
return KeyPair(
publicKey: jwk.containsKey('n') && jwk.containsKey('e')
? RsaPublicKey(
modulus: _base64ToInt(jwk['n']),
exponent: _base64ToInt(jwk['e']),
)
: null,
privateKey: jwk.containsKey('n') &&
jwk.containsKey('d') &&
jwk.containsKey('p') &&
jwk.containsKey('q')
? RsaPrivateKey(
modulus: _base64ToInt(jwk['n']),
privateExponent: _base64ToInt(jwk['d']),
firstPrimeFactor: _base64ToInt(jwk['p']),
secondPrimeFactor: _base64ToInt(jwk['q']),
)
: null);
case 'EC':
final curve = _parseCurve(jwk['crv']);
return KeyPair(
privateKey: jwk.containsKey('d') && curve != null
? EcPrivateKey(
eccPrivateKey: _base64ToInt(jwk['d']),
curve: curve,
)
: null,
publicKey:
jwk.containsKey('x') && jwk.containsKey('y') && curve != null
? EcPublicKey(
xCoordinate: _base64ToInt(jwk['x']),
yCoordinate: _base64ToInt(jwk['y']),
curve: curve,
)
: null,
);
case 'OKP':
// RFC 8037 ยง2: `x`/`d` are base64url of the RAW octet string, decoded
// as bytes (NOT via `_base64ToInt`, which would corrupt the key).
final curve = _parseOkpCurve(jwk['crv']);
if (curve == null) return null;
return KeyPair(
publicKey: jwk.containsKey('x')
? OkpPublicKey(
rawBytes: Uint8List.fromList(_base64ToBytes(jwk['x'])),
curve: curve,
)
: null,
privateKey: jwk.containsKey('d')
? OkpPrivateKey(
rawBytes: Uint8List.fromList(_base64ToBytes(jwk['d'])),
curve: curve,
)
: null,
);
}
return null;
}