package com.mindprod.example;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Example use of SHA-1 with java.security.MessageDigest. Demonstrate how to compute a SHA-1 message digest.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0
 */
public final class TestSHA
    {
// --------------------------- main() method ---------------------------

    /**
     * Compute SHA-1 digest
     *
     * @param args not used
     * @throws UnsupportedEncodingException if 8859_1 not supported.
     * @throws NoSuchAlgorithmException     if SHA not supported.
     */
    public static void main( String[] args ) throws UnsupportedEncodingException, NoSuchAlgorithmException
        {
        byte[] theTextToDigestAsBytes =
                "The quick brown fox jumped over the lazy dog's back"
                        .getBytes( "8859_1"/* encoding */ );
        MessageDigest md = MessageDigest.getInstance( "SHA" );
        md.update( theTextToDigestAsBytes );
        byte[] digest = md.digest();

        // will print SHA
        System.out.println( "Algorithm used:" + md.getAlgorithm() );

        // should be 20 bytes, 160 bits long
        System.out.println( digest.length );

        // dump out the hash
        for ( byte b : digest )
            {
            System.out.print( Integer.toHexString( b & 0xff ) + " " );
            }
        }
    }