HmacSHA1 é um MAC, não um Cipher ou um MessageDigest.
Não sei se existe na JCE da IBM, mas na da Sun existe com certeza.
Este programa é uma modificação de um teste que fiz para ver se o HMAC-MD5 (que está definida em RFC-2104) funcionava. Os valores de digest que aparecem no comentário estão corrigidos para HMAC-SHA1.
import javax.crypto.*;
import javax.crypto.spec.*;
public class TestHMAC2 {
/*
Test vectors from RFC 2104 (HMAC)
key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
key_len = 16 bytes
data = "Hi There"
data_len = 8 bytes
digest = 0x675b0b3a1b4ddf4e124872da6c2f632bfed957e9
key = "Jefe"
data = "what do ya want for nothing?"
data_len = 28 bytes
digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
key = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
key_len 16 bytes
data = 0xDDDDDDDDDDDDDDDDDDDD...
..DDDDDDDDDDDDDDDDDDDD...
..DDDDDDDDDDDDDDDDDDDD...
..DDDDDDDDDDDDDDDDDDDD...
..DDDDDDDDDDDDDDDDDDDD
data_len = 50 bytes
digest = 0xd730594d167e35d5956fd8003d0db3d3f46dc7bb
*/
public static void main(String[] args) throws Exception {
SecretKey sks;
Mac hmac;
hmac = Mac.getInstance("HmacSHA1");
sks = new SecretKeySpec (hexToBytes ("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), "HmacSHA1");
hmac.init (sks);
System.out.println (bytesToHex (hmac.doFinal("Hi There".getBytes())));
sks = new SecretKeySpec ("Jefe".getBytes(), "HmacSHA1");
hmac.init (sks);
System.out.println (bytesToHex (hmac.doFinal("what do ya want for nothing?".getBytes())));
sks = new SecretKeySpec (hexToBytes ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), "HmacSHA1");
hmac.init (sks);
System.out.println (bytesToHex (hmac.doFinal(hexToBytes ("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"))));
}
private static byte[] hexToBytes(String s) {
byte[] ret = new byte[s.length()/2];
for (int i = 0; i < ret.length; ++i) {
ret[i] = (byte)Integer.parseInt(s.substring(i*2,i*2+2),16);
}
return ret;
}
private static final char[] hex = "0123456789abcdef".toCharArray();
private static String bytesToHex(byte[] b) {
char[] ret = new char[b.length*2];
for (int i = 0, j = 0; i < b.length; ++i) {
ret[j++] = hex[(b[i] & 0xF0)>>4];
ret[j++] = hex[b[i] & 0xF];
}
return new String(ret);
}
}