I'm trying to sign my message to Amazon AWS (inside JUnit test), but I encountered a problem. Here's the code I'm using:
String secretAccessKey = "secret1234678901";
SecretKeySpec keySpec = new SecretKeySpec(secretAccessKey.getBytes(UTF-8), "HmacSHA256");
Mac mac = Mac.getInstance(this.MAC_ALGO);
mac.init(keySpec); // here it breaks
byte[] encoded = mac.doFinal(
request.toString().getBytes(this.CHARSET));
return Base64.encodeBase64URLSafeString(encoded);
In the line marked (mac.init(...)
) java throws exception:
java.lang.ClassCastException: com.sun.crypto.provider.HmacSHA1 cannot be cast to javax.crypto.MacSpi
at javax.crypto.Mac.a(DashoA13*..)
at javax.crypto.Mac.init(DashoA13*..)
Do you know why it happens? All the codes I've seen on the net look almost exactly like this, I also tried with HmacSHA1, with same results.
Sorry I didn't add everything. The code above was tested using junit and powermockito. But powermockito can't enhance javax.crypto classes so I had to add @PowerMockIgnore("javax.crypto.*")
to the junit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With