Skip to content

Instantly share code, notes, and snippets.

@HCLarsen
Created October 23, 2018 00:24
Show Gist options
  • Save HCLarsen/4432fe86a180e39fb5e73393a1c83f72 to your computer and use it in GitHub Desktop.
Save HCLarsen/4432fe86a180e39fb5e73393a1c83f72 to your computer and use it in GitHub Desktop.
require 'jose'
# HS256 Symmetric Signature
secret = 'some128bitsecret'
jwk = JOSE::JWK.from_oct(secret)
header = { "alg" => "HS256" }
payload = { "iss" => "Chris Larsen",
"sub" => "JWTs",
"aud" => "Silicon Halton Software P2P",
"iat" => 1540121863 }
signed_hs256 = JOSE::JWT.sign(jwk, header, payload).compact
puts "Signed and Encoded JWS: #{signed_hs256}"
verification = JOSE::JWT.verify_strict(jwk, ["HS256"], signed_hs256)
puts "Verification of JWS: #{verification.first}"
# RS256 Assymetric Signature
private_key = JOSE::JWK.generate_key([:rsa, 4096])
public_key = JOSE::JWK.to_public(private_key)
header = { "alg" => "RS256" }
signed_rs256 = JOSE::JWT.sign(private_key, header, payload).compact
puts "Signed and Encoded JWS: #{signed_rs256}"
verification = JOSE::JWT.verify_strict(public_key, ["RS256"], signed_rs256)
puts "Verification of JWS: #{verification.first}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment