Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.LinkedList;

public class Main {
    /**
     * Generate a hash chain of a random number
     * @param r1
     * @param numHash
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static LinkedList<BigInteger> getHashChain(BigInteger r1, int numHash)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {

        LinkedList<BigInteger> hashChain = new LinkedList<BigInteger>();
        hashChain.add(r1);

        for (int i = 1; i < numHash; i++) {
            byte[] lastR = hashChain.getLast().toByteArray();
            hashChain.add(new BigInteger(1, SHA1(lastR)));
        }

        return hashChain;
    }

    /**
     * Generate the SHA1 hash of a message 
     * @param message
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static byte[] SHA1(byte[] message) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-1");
        md.update(message);
        return md.digest();
    }
}