16,471
社区成员
发帖
与我相关
我的任务
分享
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class HMAC_SHA1 {
public static void main(String[] args) {
System.out.println(base64sha1("message", "234123"));
}
public static String base64sha1(String str, String key) {
try {
String Algorithm = "HmacSHA1";
SecretKey sk = new SecretKeySpec(key.getBytes(), Algorithm);
Mac mac = Mac.getInstance(Algorithm);
mac.init(sk);
byte[] result = mac.doFinal(str.getBytes());
for (byte b : result) {
System.out.print(b);
System.out.print(" ");
}
return new String(Base64.encodeBase64(result));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return key;
}
}
#include <openssl/sha.h>