net.mamian.mySpringboot.utils.SecurityUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.mamian.mySpringboot.utils.SecurityUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package net.mamian.mySpringboot.utils;

import java.text.DateFormat;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;

/**
 * 
 *
 * @author mamian
 * @mail mamianskyma@aliyun.com
 * @date 2016-7-26 10:32:55
 * @copyright 2016 ? All Rights Reserved
 */
public class SecurityUtils {

    private static final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT);

    /**
     * ??
     * Base64(now + identity)identitynull??
     *
     * @param identity can be null
     * @return
     */
    public static String getSalt(String identity) {
        String now = df.format(new Date());
        String identityString = StringUtils.isBlank(identity) ? RandomStringUtils.randomAlphanumeric(20)
                : identity.trim();
        return Base64.encodeBase64String(blend(now, identityString));
    }

    /**
     * ?
     *
     * @param salt
     * @param userPassword
     * @return
     */
    public static String getPassphrase(String salt, String userPassword) {
        return DigestUtils.shaHex(blend(salt, userPassword));
    }

    /**
     * ?
     */
    private static byte[] blend(String a, String b) {
        return a.concat(b).getBytes();
    }

}