Showing posts with label sha1. Show all posts
Showing posts with label sha1. Show all posts

Friday, October 10, 2014

Java Code for Calculating HMAC-SHA1 & HMAC-SHA256 Signature

package com.pritom.kumar;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by pritom on 09/10/2014.
 */
public class HmacSha1 {
    public static void main(String[] args) {
        System.out.println(hmacSha1("mykey", "helloworld"));
        System.out.println(hmacSha256("mykey", "helloworld"));
    }

    public static String hmacSha1(String KEY, String VALUE) {
        return hmacSha(KEY, VALUE, "HmacSHA1");
    }

    public static String hmacSha256(String KEY, String VALUE) {
        return hmacSha(KEY, VALUE, "HmacSHA256");
    }

    private static String hmacSha(String KEY, String VALUE, String SHA_TYPE) {
        try {
            SecretKeySpec signingKey = new SecretKeySpec(KEY.getBytes("UTF-8"), SHA_TYPE);
            Mac mac = Mac.getInstance(SHA_TYPE);
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(VALUE.getBytes("UTF-8"));

            byte[] hexArray = {
                    (byte)'0', (byte)'1', (byte)'2', (byte)'3',
                    (byte)'4', (byte)'5', (byte)'6', (byte)'7',
                    (byte)'8', (byte)'9', (byte)'a', (byte)'b',
                    (byte)'c', (byte)'d', (byte)'e', (byte)'f'
            };
            byte[] hexChars = new byte[rawHmac.length * 2];
            for ( int j = 0; j < rawHmac.length; j++ ) {
                int v = rawHmac[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
            return new String(hexChars);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

Output would be like this:

74ae5a4a3d9996d5918defc2c3d475471bbf59ac
7fdfaa9c9c0931f52d9ebf2538bc99700f2e771f3af1c1d93945c2256c11aedd

Monday, April 22, 2013

Convert string to sha1 using java

<?php
public static String hash(String toHash) {
    if (toHash == null || toHash.equals(""))
        return null;
    
    StringBuffer hexString = new StringBuffer();
    
    try {
        java.io.StringReader sr =  new java.io.StringReader(toHash);
        java.io.ByteArrayOutputStream baos =  new java.io.ByteArrayOutputStream();
        int ch;

        while ((ch = sr.read()) != -1) {
            baos.write(ch);
        }

         //SHA-1 Hash value of data 
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        sha.reset();
        sha.update(baos.toByteArray());

        byte[] digest = sha.digest();

        /*  NB: This conversion strips the leading 0 from any 
         *  digest byte with a value less than 16.
         *  
         *  This weakens the security of the SHA-1 hash and requires
         *  workarounds on the server to overcome this.
         *  
         *  It's all quite embarrassing. -- PB
         */
        for (int i=0;i<digest.length;i++) {
            hexString.append(Integer.toHexString(0xFF & digest[i]).toUpperCase());
        }
    } catch (java.io.IOException ioE) {
        System.out.println(ioE);
    } catch (NoSuchAlgorithmException algE) {
        System.out.println(algE);
    }
    
    return hexString.toString();
}
?>