- hash_hmac()
1 Java[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Java
Copy
public static String hashHmacSha1(String value, String key) {
try {
byte[] hexBytes = new Hex().encode(hashHmacSha1Raw(value,key));
return new String(hexBytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] hashHmacSha1Raw(String value, String key) {
try {
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
return mac.doFinal(value.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println( hashHmacSha1( "hello world", "secret" ) );
// 03376ee7ad7bbfceee98660439a4d8b125122a5a
System.out.println( new String(hashHmacSha1Raw( "hello world", "secret" )) );
// �7n�{���f�9�ر%�*Z
}
2 PHP[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
PHP
Copy
echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
# b8e7ae12510bdfb1812e463a7f086122cf37e4f7
PHP
Copy
echo hash_hmac( 'sha1', 'hello world', 'secret' );
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
PHP
Copy
$h = hash_hmac( 'sha1', 'hello world', 'secret', true );
echo base64_encode( $h );
# Azdu5617v87umGYEOaTYsSUSKlo=
3 Python[ | ]
Python
Copy
import hmac
import base64
h = hmac.new('hello world').digest()
print( base64.b64encode( h ) )
# sGXfsWEMawIp9we7B/iuuA==
Python
Copy
import hmac
import base64
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).hexdigest()
print( h )
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
Python
Copy
import hmac
import base64
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).digest()
print( base64.b64encode( h ) )
# Azdu5617v87umGYEOaTYsSUSKlo=
4 Perl[ | ]
Perl
Copy
use Digest::SHA qw(hmac_sha1_base64);
my $digest = hmac_sha1_base64("hello world", "secret");
while (length($digest) % 4) {
$digest .= '=';
}
print $digest . "\n";
# Azdu5617v87umGYEOaTYsSUSKlo=