SHA-1 em Java

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public class sha1it {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        if (args.length==0) return;
 
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(args[0].getBytes());
        byte[] newHash = md.digest();
        StringBuffer newHashStringBuffer = new StringBuffer();
        for ( int i = 0; i < newHash.length; i++ )
            newHashStringBuffer.append(Integer.toHexString(newHash[i]&0xff));
        System.out.println(newHashStringBuffer.toString());
    }
}

Vai calcular o SHA-1 do primeiro argumento da linha de comando:

$ java sha1it silveira
665fc8e2ed1b8aa1571a5c9d3e14d89bad4f0

Note que não é uma implementação pura do SHA-1. Simplesmente pedimos por essa implementação, que pode não estar disponível.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">