Attempt to generate a secure hash using the passed object as part of a seed, along with a timestamp. - Java Security

Java examples for Security:Hash

Description

Attempt to generate a secure hash using the passed object as part of a seed, along with a timestamp.

Demo Code


//package com.java2s;
import java.security.SecureRandom;
import java.io.Serializable;
import java.nio.ByteBuffer;

public class Main {
    private static SecureRandom randGen = new SecureRandom(ByteBuffer
            .allocate(8).putLong(System.nanoTime()).array());

    /**/*from w  w w  .  j a  va 2 s  .  c  o m*/
     * Attempt to generate a secure hash using the passed object as
     * <em>part</em> of a seed, along with a timestamp. 
     * <em>NOTE:</em> Currently we're just using the time as a seed but
     * eventually we should use the object and maybe some other system data.
     */
    public static long genSecureHash(Serializable seed) {
        if (seed == null) {
            randGen.setSeed(ByteBuffer.allocate(8)
                    .putLong(System.nanoTime()).array());
        } else {
            randGen.setSeed(ByteBuffer.allocate(8)
                    .putLong(System.nanoTime()).array());
        }
        byte[] randBytes = new byte[8];
        randGen.nextBytes(randBytes);
        long hash = ByteBuffer.wrap(randBytes).getLong();
        return hash;
    }

    /**
     * Attempt to generate a secure hash using the passed object as
     * <em>part</em> of a seed, along with a timestamp. 
     * <em>NOTE:</em> Currently we're just using the time as a seed but
     * eventually we should use the object and maybe some other system data.
     */
    public static long genSecureHash() {
        return genSecureHash(null);
    }
}

Related Tutorials