@@ -13,6 +13,11 @@ module Encryption
1313 # JWE Crypto class provide RSA/AES encrypt/decrypt methods
1414 #
1515 class JweCrypto
16+ #
17+ # Create a new instance with the provided config
18+ #
19+ # @param [Hash] config configuration object
20+ #
1621 def initialize ( config )
1722 @encoding = config [ 'dataEncoding' ]
1823 @cert = OpenSSL ::X509 ::Certificate . new ( IO . binread ( config [ 'encryptionCertificate' ] ) )
@@ -25,6 +30,13 @@ def initialize(config)
2530 @public_key_fingerprint = compute_public_fingerprint
2631 end
2732
33+ #
34+ # Perform data encryption:
35+ #
36+ # @param [String] data json string to encrypt
37+ #
38+ # @return [Hash] encrypted data
39+ #
2840 def encrypt_data ( data :)
2941 cek = SecureRandom . random_bytes ( 32 )
3042 iv = SecureRandom . random_bytes ( 12 )
@@ -50,6 +62,13 @@ def encrypt_data(data:)
5062 }
5163 end
5264
65+ #
66+ # Perform data decryption
67+ #
68+ # @param [String] encrypted_data encrypted data to decrypt
69+ #
70+ # @return [String] Decrypted JSON object
71+ #
5372 def decrypt_data ( encrypted_data :)
5473 parts = encrypted_data . split ( '.' )
5574 encrypted_header , encrypted_key , initialization_vector , cipher_text , authentication_tag = parts
@@ -89,27 +108,65 @@ def decrypt_data(encrypted_data:)
89108
90109 private
91110
111+ #
112+ # Compute the fingerprint for the provided public key
113+ #
114+ # @return [String] the computed fingerprint encoded using the configured encoding
115+ #
92116 def compute_public_fingerprint
93117 OpenSSL ::Digest ::SHA256 . new ( @cert . public_key . to_der ) . to_s
94118 end
95119
120+ #
121+ # Generate the JWE header for the provided encryption algorithm and encryption method
122+ #
123+ # @param [String] alg the cryptographic algorithm used to encrypt the value of the CEK
124+ # @param [String] enc the content encryption algorithm used to perform authenticated encryption on the plaintext
125+ #
126+ # @return [Hash] the JWE header
127+ #
96128 def generate_header ( alg , enc )
97129 { alg : alg , enc : enc , kid : @public_key_fingerprint , cty : 'application/json' }
98130 end
99131
100- def jwe_encode ( payload )
101- ::Base64 . urlsafe_encode64 ( payload ) . delete ( '=' )
132+ #
133+ # URL safe Base64 encode the provided value
134+ #
135+ # @param [String] value to be encoded
136+ #
137+ # @return [String] URL safe Base64 encoded value
138+ #
139+ def jwe_encode ( value )
140+ ::Base64 . urlsafe_encode64 ( value ) . delete ( '=' )
102141 end
103142
104- def jwe_decode ( payload )
105- padlen = 4 - ( payload . length % 4 )
143+ #
144+ # URL safe Base64 decode the provided value
145+ #
146+ # @param [String] value to be decoded
147+ #
148+ # @return [String] URL safe Base64 decoded value
149+ #
150+ def jwe_decode ( value )
151+ padlen = 4 - ( value . length % 4 )
106152 if padlen < 4
107153 pad = '=' * padlen
108- payload += pad
154+ value += pad
109155 end
110- ::Base64 . urlsafe_decode64 ( payload )
156+ ::Base64 . urlsafe_decode64 ( value )
111157 end
112158
159+ #
160+ # Generate JWE compact payload from the provided values
161+ #
162+ # @param [String] hdr JWE header
163+ # @param [String] cek content encryption key
164+ # @param [String] content cipher text
165+ # @param [String] iv initialization vector
166+ # @param [String] tag cipher auth tag
167+ #
168+ # @return [String] URL safe Base64 decoded value
169+ #
113170 def generate_serialization ( hdr , cek , content , iv , tag )
114171 [ hdr , cek , iv , content , tag ] . map { |piece | jwe_encode ( piece ) } . join '.'
115172 end
0 commit comments