Skip to content

Instantly share code, notes, and snippets.

@tsondt
Created January 22, 2018 17:21
Show Gist options
  • Save tsondt/581c45cd72e9c82742f6294ed5d15859 to your computer and use it in GitHub Desktop.
Save tsondt/581c45cd72e9c82742f6294ed5d15859 to your computer and use it in GitHub Desktop.
JOSE example
require 'jose'
class User
attr_accessor :email, :puid, :ec_key
def initialize(email, puid, ec_key = ECKey.new)
@email = email
@puid = puid
@ec_key = ec_key
end
end
class ECKey
attr_accessor :private_key
attr_reader :public_key
def initialize(private_key = JOSE::JWK.generate_key([:ec, "P-384"]))
@private_key = private_key
@public_key = JOSE::JWK.to_public(private_key)
end
end
data = {"key": "value"}
jimmy = User.new("jimmy@mailsac.com","jimmy_puid")
billy = User.new("billy@mailsac.com","billy_puid")
encrypted_data = JOSE::JWE.block_encrypt([billy.ec_key.public_key,jimmy.ec_key.private_key],
data.to_s,
{"alg" => "ECDH-ES+A256KW", "enc" => "A256GCM"})
p encrypted_data
decrypted_data = JOSE::JWE.block_decrypt([jimmy.ec_key.public_key,billy.ec_key.private_key],
encrypted_data).first
p decrypted_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment