com.dcits.govsbu.southernbase.baseproject2.modules.utils.Digests.java Source code

Java tutorial

Introduction

Here is the source code for com.dcits.govsbu.southernbase.baseproject2.modules.utils.Digests.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.dcits.govsbu.southernbase.baseproject2.modules.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.zip.CRC32;

import org.apache.commons.lang3.Validate;
import com.dcits.govsbu.southernbase.baseproject2.modules.constants.Charsets;

import com.google.common.hash.Hashing;

/**
 * ??.
 * 
 * ?SHA-1/MD5byte[](?Encodes?Hex, Base64UrlSafeBase64),?salt.
 * 
 * ?crc32murmur32?int.
 * 
 * @author calvin
 */
public class Digests {

    public static final String SHA1 = "SHA-1";
    public static final String MD5 = "MD5";

    private static SecureRandom random = new SecureRandom();

    /**
     * sha1.
     */
    public static byte[] sha1(byte[] input) {
        return digest(input, SHA1, null, 1);
    }

    /**
     * md5.
     */
    public static byte[] md5(byte[] input) {
        return digest(input, MD5, null, 1);
    }

    /**
     * sha1.
     */
    public static byte[] sha1(String input) {
        return digest(input.getBytes(Charsets.UTF8), SHA1, null, 1);
    }

    /**
     * sha1.
     */
    public static byte[] sha1(String input, Charset charset) {
        return digest(input.getBytes(charset), SHA1, null, 1);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(byte[] input, byte[] salt) {
        return digest(input, SHA1, salt, 1);
    }

    /**
     * md5salt.
     */
    public static byte[] md5(byte[] input, byte[] salt) {
        return digest(input, MD5, salt, 1);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(String input, byte[] salt) {
        return digest(input.getBytes(Charsets.UTF8), SHA1, salt, 1);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(String input, Charset charset, byte[] salt) {
        return digest(input.getBytes(charset), SHA1, salt, 1);
    }

    /**
     * md5salt.
     */
    public static byte[] md5(String input, Charset charset, byte[] salt) {
        return digest(input.getBytes(charset), MD5, salt, 1);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
        return digest(input, SHA1, salt, iterations);
    }

    /**
     * md5salt.
     */
    public static byte[] md5(byte[] input, byte[] salt, int iterations) {
        return digest(input, MD5, salt, iterations);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(String input, byte[] salt, int iterations) {
        return digest(input.getBytes(Charsets.UTF8), SHA1, salt, iterations);
    }

    /**
     * md5salt.
     */
    public static byte[] md5(String input, byte[] salt, int iterations) {
        return digest(input.getBytes(Charsets.UTF8), MD5, salt, iterations);
    }

    /**
     * sha1salt.
     */
    public static byte[] sha1(String input, Charset charset, byte[] salt, int iterations) {
        return digest(input.getBytes(charset), SHA1, salt, iterations);
    }

    /**
     * md5salt.
     */
    public static byte[] md5(String input, Charset charset, byte[] salt, int iterations) {
        return digest(input.getBytes(charset), MD5, salt, iterations);
    }

    /**
     * , ?md5sha1.
     */
    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);

            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for (int i = 1; i < iterations; i++) {
                digest.reset();
                result = digest.digest(result);
            }
            return result;
        } catch (GeneralSecurityException e) {
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * ??Byte[]salt.
     * 
     * @param numBytes salt?
     */
    public static byte[] generateSalt(int numBytes) {
        Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);

        byte[] bytes = new byte[numBytes];
        random.nextBytes(bytes);
        return bytes;
    }

    /**
     * md5.
     */
    public static byte[] md5(InputStream input) throws IOException {
        return digest(input, MD5);
    }

    /**
     * sha1.
     */
    public static byte[] sha1(InputStream input) throws IOException {
        return digest(input, SHA1);
    }

    private static byte[] digest(InputStream input, String algorithm) throws IOException {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            int bufferLength = 8 * 1024;
            byte[] buffer = new byte[bufferLength];
            int read = input.read(buffer, 0, bufferLength);

            while (read > -1) {
                messageDigest.update(buffer, 0, read);
                read = input.read(buffer, 0, bufferLength);
            }

            return messageDigest.digest();
        } catch (GeneralSecurityException e) {
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * crc32.
     */
    public static int crc32(byte[] input) {
        CRC32 crc32 = new CRC32();
        crc32.update(input);
        return (int) crc32.getValue();
    }

    /**
     * crc32.
     */
    public static int crc32(String input) {
        CRC32 crc32 = new CRC32();
        crc32.update(input.getBytes(Charsets.UTF8));
        return (int) crc32.getValue();
    }

    /**
     * crc32.
     */
    public static int crc32(String input, Charset charset) {
        CRC32 crc32 = new CRC32();
        crc32.update(input.getBytes(charset));
        return (int) crc32.getValue();
    }

    /**
     * crc32php64bitlong
     */
    public static long crc32AsLong(byte[] input) {
        CRC32 crc32 = new CRC32();
        crc32.update(input);
        return crc32.getValue();
    }

    /**
     * crc32php64bitlong
     */
    public static long crc32AsLong(String input) {
        CRC32 crc32 = new CRC32();
        crc32.update(input.getBytes(Charsets.UTF8));
        return crc32.getValue();
    }

    /**
     * crc32php64bitlong
     */
    public static long crc32AsLong(String input, Charset charset) {
        CRC32 crc32 = new CRC32();
        crc32.update(input.getBytes(charset));
        return crc32.getValue();
    }

    /**
     * murmur32
     */
    public static int murmur32(byte[] input) {
        return Hashing.murmur3_32().hashBytes(input).asInt();
    }

    /**
     * murmur32
     */
    public static int murmur32(String input) {
        return Hashing.murmur3_32().hashString(input, Charsets.UTF8).asInt();
    }

    /**
     * murmur32
     */
    public static int murmur32(String input, Charset charset) {
        return Hashing.murmur3_32().hashString(input, charset).asInt();
    }

    /**
     * murmur32seed
     */
    public static int murmur32(byte[] input, int seed) {
        return Hashing.murmur3_32(seed).hashBytes(input).asInt();
    }

    /**
     * murmur32seed
     */
    public static int murmur32(String input, int seed) {
        return Hashing.murmur3_32(seed).hashString(input, Charsets.UTF8).asInt();
    }

    /**
     * murmur32seed
     */
    public static int murmur32(String input, Charset charset, int seed) {
        return Hashing.murmur3_32(seed).hashString(input, charset).asInt();
    }
}