lib/oydid/didcomm.rb in oydid-0.4.1 vs lib/oydid/didcomm.rb in oydid-0.4.3
- old
+ new
@@ -16,17 +16,54 @@
dcDoc["body"] = payload
return [dcDoc, ""]
end
- # signing -----------------------------------
- def self.msg_encrypt(payload, private_key_encoded)
+ # DIDComm Signed Message --------------------
+ def self.dcsm(payload, private_key_encoded, options)
error = ""
code, length, digest = decode(private_key_encoded).unpack('SCa*')
case Multicodecs[code].name
when 'ed25519-priv'
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
+ token = JWT.encode payload, private_key, 'ED25519', { typ: 'JWM', kid: options[:sign_did].to_s, alg: 'ED25519' }
+ else
+ token = nil
+ error = "unsupported key codec"
+ end
+ return [token, error]
+ end
+
+ def self.dcsm_verify(token, options)
+ error = ""
+ decoded_payload = JWT.decode token, nil, false
+ pubkey_did = decoded_payload.last["kid"]
+ result, msg = Oydid.read(pubkey_did, options)
+ public_key_encoded = Oydid.w3c(result, options)["authentication"].first["publicKeyMultibase"]
+ begin
+ code, length, digest = Oydid.decode(public_key_encoded).unpack('CCa*')
+ case Multicodecs[code].name
+ when 'ed25519-pub'
+ public_key = RbNaCl::Signatures::Ed25519::VerifyKey.new(digest)
+ payload = JWT.decode token.to_s, public_key, true, { algorithm: 'ED25519' }
+ else
+ payload = nil
+ error = "unsupported key codec"
+ end
+ return [payload, error]
+ rescue
+ return [nil, "verification failed"]
+ end
+ end
+
+ # encryption -----------------------------------
+ def self.msg_encrypt(payload, private_key_encoded, did)
+ error = ""
+ code, length, digest = decode(private_key_encoded).unpack('SCa*')
+ case Multicodecs[code].name
+ when 'ed25519-priv'
+ private_key = RbNaCl::Signatures::Ed25519::SigningKey.new(digest)
token = JWT.encode payload, private_key, 'ED25519'
else
token = nil
error = "unsupported key codec"
end
@@ -45,6 +82,20 @@
error = "unsupported key codec"
end
return [payload, error]
end
+ # signing for JWS ---------------------------
+ def self.msg_sign(payload, hmac_secret)
+ token = JWT.encode payload, hmac_secret, 'HS256'
+ return [token, ""]
+ end
+
+ def self.msg_verify_jws(token, hmac_secret)
+ begin
+ decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
+ return [decoded_token, ""]
+ rescue
+ return [nil, "verification failed"]
+ end
+ end
end
\ No newline at end of file