@@ -9,5 +9,70 @@ def ==(other)
99
1010 OpenSSL . fixed_length_secure_compare ( self . digest , other . digest )
1111 end
12+
13+ # :call-seq:
14+ # hmac.base64digest -> string
15+ #
16+ # Returns the authentication code an a Base64-encoded string.
17+ def base64digest
18+ [ digest ] . pack ( "m0" )
19+ end
20+
21+ class << self
22+ # :call-seq:
23+ # HMAC.digest(digest, key, data) -> aString
24+ #
25+ # Returns the authentication code as a binary string. The _digest_ parameter
26+ # specifies the digest algorithm to use. This may be a String representing
27+ # the algorithm name or an instance of OpenSSL::Digest.
28+ #
29+ # === Example
30+ # key = 'key'
31+ # data = 'The quick brown fox jumps over the lazy dog'
32+ #
33+ # hmac = OpenSSL::HMAC.digest('SHA1', key, data)
34+ # #=> "\xDE|\x9B\x85\xB8\xB7\x8A\xA6\xBC\x8Az6\xF7\n\x90p\x1C\x9D\xB4\xD9"
35+ def digest ( digest , key , data )
36+ hmac = new ( key , digest )
37+ hmac << data
38+ hmac . digest
39+ end unless method_defined? ( :digest ) # JRuby
40+
41+ # :call-seq:
42+ # HMAC.hexdigest(digest, key, data) -> aString
43+ #
44+ # Returns the authentication code as a hex-encoded string. The _digest_
45+ # parameter specifies the digest algorithm to use. This may be a String
46+ # representing the algorithm name or an instance of OpenSSL::Digest.
47+ #
48+ # === Example
49+ # key = 'key'
50+ # data = 'The quick brown fox jumps over the lazy dog'
51+ #
52+ # hmac = OpenSSL::HMAC.hexdigest('SHA1', key, data)
53+ # #=> "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"
54+ def hexdigest ( digest , key , data )
55+ hmac = new ( key , digest )
56+ hmac << data
57+ hmac . hexdigest
58+ end unless method_defined? ( :hexdigest ) # JRuby
59+
60+ # :call-seq:
61+ # HMAC.base64digest(digest, key, data) -> aString
62+ #
63+ # Returns the authentication code as a Base64-encoded string. The _digest_
64+ # parameter specifies the digest algorithm to use. This may be a String
65+ # representing the algorithm name or an instance of OpenSSL::Digest.
66+ #
67+ # === Example
68+ # key = 'key'
69+ # data = 'The quick brown fox jumps over the lazy dog'
70+ #
71+ # hmac = OpenSSL::HMAC.base64digest('SHA1', key, data)
72+ # #=> "3nybhbi3iqa8ino29wqQcBydtNk="
73+ def base64digest ( digest , key , data )
74+ [ digest ( digest , key , data ) ] . pack ( "m0" )
75+ end
76+ end
1277 end
1378end
0 commit comments