com.my.quickstart.util.Digests.java Source code

Java tutorial

Introduction

Here is the source code for com.my.quickstart.util.Digests.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.my.quickstart.util;

import java.security.SecureRandom;
import java.util.UUID;

import org.apache.commons.lang3.Validate;
import org.springframework.security.crypto.keygen.KeyGenerators;

/**
 * ByteSource??Hex, Base64UrlSafeBase64
 */
public class Digests {
    private static SecureRandom random = new SecureRandom();

    /**
     * ?UUIDsalt
     * @return
     */
    public static String generateUUIDSalt() {
        byte[] b = KeyGenerators.secureRandom(16).generateKey();
        String salt = UUID.nameUUIDFromBytes(b).toString();
        return salt;
    }

    /**
     * ??Byte[]salt.
     * 
     * @param numBytes byte?
     */
    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;
    }
}