Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

trying to generate a HMAC MD5 hash

843810Sep 7 2004 — edited Aug 12 2006

Hi, I'm trying to create a fingerprint to send to a credit card processor (authorizenet). I'm using the JCE and the code below Is all I could come up with looking online and the API.

I cannot seem to figure out how to use MY OWN key that the gateway I need to connect to provided me. All the examples I see online generate the key and I can't find any examples where you use your own key?
public String HMAC(String values, String key){
    String output = "";
    try {
        
        //Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
        //In practice, you would save this key.
        //KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
        //SecretKey key = keyGen.generateKey();
        
        SecretKey key = "AUNSH8FfjkDKSHJBCD"; // <----- how can I get this to compile?
    
        // Create a MAC object using HMAC-MD5 and initialize with key
        Mac mac = Mac.getInstance(key.getAlgorithm());
        mac.init(key);
    
    
        // Encode the string into bytes using utf-8 and digest it
        byte[] utf8 = values.getBytes("UTF8");
        byte[] digest = mac.doFinal(utf8);
    
        // If desired, convert the digest into a string
        String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
        output += digestB64;
        
        } catch (InvalidKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        }
        
        return output;
    }
thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2006
Added on Sep 7 2004
13 comments
639 views