Java URI Hash Link Generation Sample

Plain HTML / Printer friendly.Plain HTML / Printer friendly.

Sample code to generate the URI hash link using the Java programming language...

 

import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public static void main(String[] args) {
    long expires = new Date().getTime(); // get unix date

    String url = "http://yourUrl.com";
    String secretKey = "yourSecretKey";
    String rawKey = "rawKey";
    String originalValue = "originalValue";

    String keyStr = url + secretKey;
    // enabled if using expiry.
    // keyStr = expires+url+secretKey;
    try {
        SecretKeySpec key = new SecretKeySpec(rawKey.getBytes(), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
        cipher.doFinal(originalValue.getBytes());

        String hashKey = new String(Base64.encodeBase64(DigestUtils.md5(keyStr))).replace("+", "-").replace("\\", "_");

        System.out.println("Key: " + keyStr + "\nHash: " + hashKey);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}