All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.starxmind.boot.ldap.SHA1Utils Maven / Gradle / Ivy

package com.starxmind.boot.ldap;

import com.starxmind.bass.sugar.Asserts;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA1工具
 *
 * @author pizzalord
 * @since 1.0
 */
public abstract class SHA1Utils {
    // 将输入用户和密码进行加密算法后验证
    public static void verifyPassword(String ldappw, String inputpw) throws NoSuchAlgorithmException {
        // MessageDigest 提供了消息摘要算法,如 MD5 或 SHA,的功能,这里LDAP使用的是SHA-1
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        // 取出加密字符
        if (ldappw.startsWith("{SSHA}")) {
            ldappw = ldappw.substring(6);
        } else if (ldappw.startsWith("{SHA}")) {
            ldappw = ldappw.substring(5);
        }

        // 解码BASE64
        byte[] ldappwbyte = Base64.decode(ldappw);
        byte[] shacode;
        byte[] salt;

        // 前20位是SHA-1加密段,20位后是最初加密时的随机明文
        if (ldappwbyte.length <= 20) {
            shacode = ldappwbyte;
            salt = new byte[0];
        } else {
            shacode = new byte[20];
            salt = new byte[ldappwbyte.length - 20];
            System.arraycopy(ldappwbyte, 0, shacode, 0, 20);
            System.arraycopy(ldappwbyte, 20, salt, 0, salt.length);
        }

        // 把用户输入的密码添加到摘要计算信息
        md.update(inputpw.getBytes());
        // 把随机明文添加到摘要计算信息
        md.update(salt);

        // 按SSHA把当前用户密码进行计算
        byte[] inputpwbyte = md.digest();

        Asserts.isTrue(MessageDigest.isEqual(shacode, inputpwbyte), "密码不正确");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy