com.apabi.r2k.common.security.util.NonceUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.apabi.r2k.common.security.util.NonceUtils.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: NonceUtils.java 762 2009-12-27 14:46:36Z calvinxiu $
 */
package com.apabi.r2k.common.security.util;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Hex;

/**
 * ?, ?????.
 * 
 * ????++?.
 * Nonce?(JDK UUID, Hibernate UUIDGenerator, Apache RandomStringUtils)?NonceUtilsTest.
 * 
 * @author calvin
 */
public abstract class NonceUtils {
    private NonceUtils() {

    }

    //RFC3339 ?,
    private static final SimpleDateFormat INTERNATE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HHmmssSSS'Z'");

    //?, ?1,2,4,8?.
    private static final String[] SPACES = { "0", "00", "0000", "00000000" };

    //?JVM?.
    private static Date lastTime;
    private static int counter = 0;

    //-- Random function --//
    /**
     * SecureRandom?, Hex?.
     * 
     * @param length ,?.
     */
    public static String randomHexString(final int length) {
        SecureRandom nonceGenerator = new SecureRandom();
        byte[] nonce = new byte[length / 2];
        nonceGenerator.nextBytes(nonce);
        return Hex.encodeHexString(nonce);
    }

    /**
     * SecureRandom?Integer.
     */
    public static int randomInt() {
        return new SecureRandom().nextInt();
    }

    /**
     * SecureRandom?Integer,?8Hex?.
     */
    public static String randomHexInt() {
        return Integer.toHexString(randomInt());
    }

    /**
     * SecureRandom?Long.
     */
    public static long randomLong() {
        return new SecureRandom().nextLong();
    }

    /**
     * SecureRandom?Long, ?16Hex?.
     */
    public static String randomHexLong() {
        return Long.toHexString(randomLong());
    }

    //-- Timestamp function --//
    /**
     * Internate??.
     * 
     * ?yyyy-MM-dd'T'HH:mm:ss.SSS'Z', 2009-10-15T14:24:50.316Z.
     */
    public static String currentTimestamp() {
        Date now = new Date();
        return INTERNATE_DATE_FORMAT.format(now);
    }

    public static void main(String[] args) {
        System.out.println(currentTimestamp());
    }

    /**
     * ??1970.
     */
    public static long currentMills() {
        return System.currentTimeMillis();
    }

    /**
     * ??1970, Hex?.
     */
    public static String currentHexMills() {
        return Long.toHexString(currentMills());
    }

    //-- Helper function --//
    /**
     * Hex??Counter.
     */
    public static synchronized String getCounter() {
        Date currentTime = new Date();

        if (currentTime.equals(lastTime)) {
            counter++;
        } else {
            lastTime = currentTime;
            counter = 0;
        }
        return Integer.toHexString(counter);
    }

    /**
     * ?, , ???0.
     */
    public static String format(final String source, final int length) {
        int spaceLength = length - source.length();
        StringBuilder buf = new StringBuilder();

        while (spaceLength >= 8) {
            buf.append(SPACES[3]);
            spaceLength -= 8;
        }

        for (int i = 2; i >= 0; i--) {
            if ((spaceLength & (1 << i)) != 0) {
                buf.append(SPACES[i]);
            }
        }

        buf.append(source);
        return buf.toString();
    }
}