class
JOSE::JWK
- JOSE::JWK
- Reference
- Object
Overview
Represents a JSON Web Key (JWK) for EC, RSA, oct, and OKP key types.
Defined in:
jose/jwk.crConstructors
-
.from_binary(json : String) : JWK
Parses json and returns the resulting
JWK. -
.from_map(map : Hash(String, JSON::Any)) : JWK
Constructs a
JWKwrapping the given map. -
.from_map(map : Hash(String, JSON::Any), **overrides : String) : JWK
Constructs a
JWKwrapping map with overrides merged in. -
.from_oct(key : Bytes) : JWK
Creates an oct (symmetric) JWK from raw key bytes.
-
.from_pem(pem : String) : JWK
Loads an EC or RSA key from PEM and returns the corresponding
JWK. -
.generate_key(params : Hash(String, JSON::Any)) : JWK
Generates a new key according to params.
-
.generate_key_ec(crv : String = "P-256") : JWK
Generates a new
"EC"key. -
.generate_key_oct(size : Int32 = 32) : JWK
Generates a new
"oct"key. -
.generate_key_okp(crv : String = "Ed25519") : JWK
Generates a new
"OKP"key. -
.generate_key_rsa(bits : Int32 = 2048) : JWK
Generates a new
"RSA"key. - .new(map : Hash(String, JSON::Any))
Instance Method Summary
-
#==(other : JWK) : Bool
Returns
trueif both JWKs have identical key parameters. -
#[](key : String) : JSON::Any
Returns the value for key from the key map.
-
#[]?(key : String) : JSON::Any | Nil
Returns the value for key, or
nilif absent. -
#block_decrypt(encrypted : String | EncryptedBinary) : String
Decrypts encrypted using this key.
-
#block_encrypt(plain_text : String, header = nil) : EncryptedBinary
Encrypts plain_text using this key.
-
#ec_private_key : LibCrypto::EC_KEY
Returns an
EC_KEYwith both public and private key set. -
#ec_public_key : LibCrypto::EC_KEY
Returns an
EC_KEYwith only the public key set. -
#ed25519_signing_key : Ed25519::SigningKey
Returns the Ed25519 signing key for
OKPprivate keys. -
#ed25519_verify_key : Ed25519::VerifyKey
Returns the Ed25519 verify key for
OKPkeys. -
#hash(hasher)
See
Object#hash(hasher) -
#key_bytes : Bytes
Returns the raw symmetric key bytes for
octkeys. -
#kty : String
Returns the key type (
"EC","RSA","oct", or"OKP"). -
#map : Hash(String, JSON::Any)
The underlying JSON map holding all key parameters.
-
#private? : Bool
Returns
trueif this key contains private key material. -
#public? : Bool
Returns
trueif this key contains no private key material. -
#rsa_raw_key : LibCryptoJose::RSA
Returns the raw
LibCryptoJose::RSAhandle built from this key's parameters. -
#sign(plain_text : String, header = nil) : SignedBinary
Signs plain_text using this key.
-
#to_binary : String
Serializes this key to a JSON string.
-
#to_map : Hash(String, JSON::Any)
Returns the underlying JSON map.
-
#to_pem : String
Serializes this key to PEM format.
-
#to_public : JWK
Returns a copy of this JWK with all private fields removed.
-
#verify(signed : String | SignedBinary) : Tuple(Bool, String)
Verifies signed using this key.
-
#with(**fields : String) : JWK
Returns a copy of this JWK with the given string fields merged in.
Constructor Detail
Parses json and returns the resulting JWK.
Constructs a JWK wrapping the given map.
Constructs a JWK wrapping map with overrides merged in.
String overrides are automatically boxed into JSON::Any.
JOSE::JWK.from_map(k1.map, kid: "sig")
Creates an oct (symmetric) JWK from raw key bytes.
Loads an EC or RSA key from PEM and returns the corresponding JWK.
Returns a JWK with private fields if the PEM contains a private key,
or a public-only JWK if it contains only a public key.
Accepted PEM formats and the OpenSSL commands that produce them:
# EC private key (SEC 1 / RFC 5915)
openssl ecparam -name prime256v1 -genkey -noout -out ec-p256.pem
# EC public key (SubjectPublicKeyInfo)
openssl ec -in ec-p256.pem -pubout -out ec-p256-pub.pem
# RSA private key (PKCS#1)
openssl genrsa -out rsa-2048.pem 2048
# RSA public key (SubjectPublicKeyInfo)
openssl rsa -in rsa-2048.pem -pubout -out rsa-2048-pub.pem
jwk = JOSE::JWK.from_pem(File.read("ec-p256.pem"))
Generates a new key according to params.
The params hash must contain at least "kty" and, depending on the
key type, optional shape keys:
"EC"—"crv": curve name ("P-256"…"P-521"), default"P-256""RSA"—"bits": key size in bits, default2048"oct"—"size": key length in bytes, default32"OKP"—"crv": curve name ("Ed25519"), default"Ed25519"
NOTE RSA key generation is synchronous and may take a moment for large bit sizes. EC and OKP generation is near-instant.
Using OpenSSL to generate keys externally:
# EC
openssl ecparam -name prime256v1 -genkey -noout -out ec-p256.pem
# RSA
openssl genrsa -out rsa-2048.pem 2048
Load with JWK.from_pem(File.read("ec-p256.pem")).
Prefer the typed convenience wrappers .generate_key_ec, .generate_key_rsa,
.generate_key_oct, and .generate_key_okp for common cases.
ec_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("EC"), "crv" => JSON::Any.new("P-384")})
rsa_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("RSA"), "bits" => JSON::Any.new(2048_i64)})
sym_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("oct"), "size" => JSON::Any.new(32_i64)})
okp_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("OKP"), "crv" => JSON::Any.new("Ed25519")})
Generates a new "EC" key.
crv defaults to "P-256". See .generate_key for details.
Generates a new "oct" key.
size defaults to 32. See .generate_key for details.
Generates a new "OKP" key.
crv defaults to "Ed25519". See .generate_key for details.
Generates a new "RSA" key.
bits defaults to 2048. See .generate_key for details.
Instance Method Detail
Decrypts encrypted using this key. Delegates to JWE.block_decrypt.
Encrypts plain_text using this key. Delegates to JWE.block_encrypt.
Returns an EC_KEY with both public and private key set.
NOTE Caller must free the returned key with LibCrypto.ec_key_free.
Returns an EC_KEY with only the public key set.
NOTE Caller must free the returned key with LibCrypto.ec_key_free.
Returns the Ed25519 signing key for OKP private keys.
Returns the raw LibCryptoJose::RSA handle built from this key's parameters.
NOTE Caller must free the returned handle with LibCryptoJose.RSA_free.
Signs plain_text using this key. Delegates to JWS.sign.
Verifies signed using this key. Delegates to JWS.verify.
Returns a copy of this JWK with the given string fields merged in. Existing fields are overwritten; all other fields are preserved.
jwk.with(kid: "sig")
jwk.with(kid: "sig", use: "sig", alg: "ES256")