Six Stars

sha1 hash key

i need to generate hash key with sha1 algorithm how can we do it with java code or sqlserver query
Raghunath Lingutla
3 REPLIES
One Star

Re: sha1 hash key

Hi
You can create Routine(hard code Java class and methods) in 'Repository-Code-Routines'.
Then call the methods of Java SHA1 implement in Talend components such as tMap, tJava and so on.
Hope this will help you.
Best regards!
Pedro
One Star

Re: sha1 hash key

Hi,
I am new to Talend and i had the same query like i have to generate hash key using SHA-1 Algorithm.So please provide me the detailed process of doing it...
Thanks
Five Stars

Re: sha1 hash key

Here's some examples of methods that you can add to a Talend routine, for doing this.
    public static String getMD5(String field) {
    if(field == null) return null;
   
    try {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
    md.reset();
    md.update(field.getBytes("UTF-8"));
    byte[] digest = md.digest();
    java.math.BigInteger bigInt = new java.math.BigInteger(1, digest);
    String hashText = bigInt.toString(16);
    // Now we need to zero pad it if you actually want the full 32 chars.
    while(hashText.length() < 32 ){
    hashText = "0" + hashText;
    }
        return hashText;
    }
    catch (Exception e) {
    return null;
    }
    }
 
    public static String getSHA256(String field) {
    if(field == null) return null;
   
    try {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");
    md.reset();
    md.update(field.getBytes("UTF-8"));
    byte[] digest = md.digest();
    java.math.BigInteger bigInt = new java.math.BigInteger(1, digest);
    String hashText = bigInt.toString(16);
    // Now we need to zero pad it if you actually want the full 32 chars.
    while(hashText.length() < 32 ){
    hashText = "0" + hashText;
    }
        return hashText;
    }
    catch (Exception e) {
    return null;
    }
    }