com.loocme.security.encrypt.Sha1 Maven / Gradle / Ivy
package com.loocme.security.encrypt;
import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author liuchi
* 简单SHA1加密
*/
@Slf4j
public class Sha1
{
/**
* 根据输入内容加密,使用数字和字母的组合进行加密
*
* @param input 明文
* @return 加密后的字符串
*/
public static String encode(final String input){
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1");
digest.update(input.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
log.error("", e);
}
return "";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy