Returns the authentication code as a binary string. The
digest
parameter must be an instance of
OpenSSL::Digest
.
Example
¶
↑
key = 'key'
data = 'The quick brown fox jumps over the lazy dog'
digest = OpenSSL::Digest.new('sha1')
hmac = OpenSSL::HMAC.digest(digest, key, data)
StringValue(key);
StringValue(data);
buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
(unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
return rb_str_new((const char *)buf, buf_len);
Returns the authentication code as a hex-encoded string. The digest parameter must be an instance of OpenSSL::Digest.
Example¶ ↑
key = 'key'
data = 'The quick brown fox jumps over the lazy dog'
digest = OpenSSL::Digest.new('sha1')
hmac = OpenSSL::HMAC.hexdigest(digest, key, data)
ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data)
unsigned char *buf;
char *hexbuf;
unsigned int buf_len;
VALUE hexdigest;
StringValue(key);
StringValue(data);
buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key),
(unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len);
if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
ossl_raise(eHMACError, "Cannot convert buf to hexbuf");
hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
return hexdigest;
Returns an instance of OpenSSL::HMAC set with the key and digest algorithm to be used. The instance represents the initial state of the message authentication code before any data has been processed. To process data with it, use the instance method update with your data as an argument.
Example¶ ↑
key = 'key'
digest = OpenSSL::Digest.new('sha1')
instance = OpenSSL::HMAC.new(key, digest)
instance.class
A note about comparisons¶ ↑
Two instances won't be equal when they're compared, even if they have the same value. Use to_s or hexdigest to return the authentication code that the instance represents. For example:
other_instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
instance
instance == other_instance
instance.to_s == other_instance.to_s
StringValue(key);
GetHMAC(self, ctx);
HMAC_Init(ctx, RSTRING_PTR(key), RSTRING_LENINT(key),
GetDigestPtr(digest));
return self;
Example¶ ↑
instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
instance.digest
GetHMAC(self, ctx);
hmac_final(ctx, &buf, &buf_len);
digest = ossl_buf2str((char *)buf, buf_len);
return digest;
GetHMAC(self, ctx);
hmac_final(ctx, &buf, &buf_len);
if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) {
OPENSSL_free(buf);
ossl_raise(eHMACError, "Memory alloc error");
OPENSSL_free(buf);
hexdigest = ossl_buf2str(hexbuf, 2 * buf_len);
return hexdigest;
Returns self as it was when it was first initialized, with all processed data cleared from it.
Example¶ ↑
data = "The quick brown fox jumps over the lazy dog"
instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
instance.update(data)
instance.reset
Returns self updated with the message to be authenticated. Can be called repeatedly with chunks of the message.
Example¶ ↑
first_chunk = 'The quick brown fox jumps '
second_chunk = 'over the lazy dog'
instance.update(first_chunk)
instance.update(second_chunk)
StringValue(data);
GetHMAC(self, ctx);
HMAC_Update(ctx, (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data));
return self;