diff options
author | Kazuki Yamaguchi <[email protected]> | 2020-06-29 22:09:35 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <[email protected]> | 2021-03-16 19:16:11 +0900 |
commit | 13198d4968a6591ec423832c4bf00c56ffdb337a (patch) | |
tree | 04bde66f568cf7aafa1af77140d8dabc8af19409 /ext/openssl/lib | |
parent | b91f62f3840582bb3e4fbece15654e1d852c829b (diff) |
[ruby/openssl] hmac: implement base64digest methods
OpenSSL::HMAC implements the similar interface as ::Digest. Let's add
base64digest methods to OpenSSL::HMAC, too, for feature parity.
https://github.com/ruby/openssl/commit/098bcb68af
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4275
Diffstat (limited to 'ext/openssl/lib')
-rw-r--r-- | ext/openssl/lib/openssl/hmac.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/ext/openssl/lib/openssl/hmac.rb b/ext/openssl/lib/openssl/hmac.rb index 9bc8bc8df3..c8c844d8d7 100644 --- a/ext/openssl/lib/openssl/hmac.rb +++ b/ext/openssl/lib/openssl/hmac.rb @@ -10,6 +10,14 @@ module OpenSSL OpenSSL.fixed_length_secure_compare(self.digest, other.digest) end + # :call-seq: + # hmac.base64digest -> string + # + # Returns the authentication code an a Base64-encoded string. + def base64digest + [digest].pack("m0") + end + class << self # :call-seq: # HMAC.digest(digest, key, data) -> aString @@ -48,6 +56,23 @@ module OpenSSL hmac << data hmac.hexdigest end + + # :call-seq: + # HMAC.base64digest(digest, key, data) -> aString + # + # Returns the authentication code as a Base64-encoded string. The _digest_ + # parameter specifies the digest algorithm to use. This may be a String + # representing the algorithm name or an instance of OpenSSL::Digest. + # + # === Example + # key = 'key' + # data = 'The quick brown fox jumps over the lazy dog' + # + # hmac = OpenSSL::HMAC.base64digest('SHA1', key, data) + # #=> "3nybhbi3iqa8ino29wqQcBydtNk=" + def base64digest(digest, key, data) + [digest(digest, key, data)].pack("m0") + end end end end |