package com.liferay.util;
import com.liferay.portal.kernel.util.StringMaker;
/**
* <a href="PwdGenerator.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
*
*/
public class PwdGenerator {
public static String KEY1 = "0123456789";
public static String KEY2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String KEY3 = "abcdefghijklmnopqrstuvwxyz";
public static String getPinNumber() {
return _getPassword(KEY1, 4);
}
public static String getPassword() {
return getPassword(8);
}
public static String getPassword(int length) {
return _getPassword(KEY1 + KEY2 + KEY3, length);
}
public static String getPassword(String key, int length) {
return _getPassword(key, length);
}
private static String _getPassword(String key, int length) {
StringMaker sm = new StringMaker();
for (int i = 0; i < length; i++) {
sm.append(key.charAt((int)(Math.random() * key.length())));
}
return sm.toString();
}
}
package com.liferay.portal.security.pwd;
import com.liferay.portal.PwdEncryptorException;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.Digester;
import com.liferay.portal.kernel.util.DigesterUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringMaker;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PropsUtil;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import org.vps.crypt.Crypt;
/**
* <a href="PwdEncryptor.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
* @author Scott Lee
*
*/
public class PwdEncryptor {
public static final String PASSWORDS_ENCRYPTION_ALGORITHM =
GetterUtil.getString(PropsUtil.get(
PropsUtil.PASSWORDS_ENCRYPTION_ALGORITHM)).toUpperCase();
public static final String TYPE_CRYPT = "CRYPT";
public static final String TYPE_MD2 = "MD2";
public static final String TYPE_MD5 = "MD5";
public static final String TYPE_NONE = "NONE";
public static final String TYPE_SHA = "SHA";
public static final String TYPE_SHA_256 = "SHA-256";
public static final String TYPE_SHA_384 = "SHA-384";
public static final String TYPE_SSHA = "SSHA";
public static final char[] saltChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
.toCharArray();
public static String encrypt(String clearTextPwd)
throws PwdEncryptorException {
return encrypt(PASSWORDS_ENCRYPTION_ALGORITHM, clearTextPwd, null);
}
public static String encrypt(String clearTextPwd, String currentEncPwd)
throws PwdEncryptorException {
return encrypt(
PASSWORDS_ENCRYPTION_ALGORITHM, clearTextPwd, currentEncPwd);
}
public static String encrypt(
String algorithm, String clearTextPwd, String currentEncPwd)
throws PwdEncryptorException {
if (algorithm.equals(TYPE_CRYPT)) {
byte[] saltBytes = _getSaltFromCrypt(currentEncPwd);
return encodePassword(algorithm, clearTextPwd, saltBytes);
}
else if (algorithm.equals(TYPE_NONE)) {
return clearTextPwd;
}
else if (algorithm.equals(TYPE_SSHA)) {
byte[] saltBytes = _getSaltFromSSHA(currentEncPwd);
return encodePassword(algorithm, clearTextPwd, saltBytes);
}
else {
return encodePassword(algorithm, clearTextPwd, null);
}
}
protected static String encodePassword(
String algorithm, String clearTextPwd, byte[] saltBytes)
throws PwdEncryptorException {
try {
if (algorithm.equals(TYPE_CRYPT)) {
return Crypt.crypt(
clearTextPwd.getBytes(Digester.ENCODING), saltBytes);
}
else if (algorithm.equals(TYPE_SSHA)) {
byte[] clearTextPwdBytes =
clearTextPwd.getBytes(Digester.ENCODING);
// Create a byte array of salt bytes appeneded to password bytes
byte[] pwdPlusSalt =
new byte[clearTextPwdBytes.length + saltBytes.length];
System.arraycopy(
clearTextPwdBytes, 0, pwdPlusSalt, 0,
clearTextPwdBytes.length);
System.arraycopy(
saltBytes, 0, pwdPlusSalt, clearTextPwdBytes.length,
saltBytes.length);
// Digest byte array
MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);
// Appends salt bytes to the SHA-1 digest.
byte[] digestPlusSalt =
new byte[pwdPlusSaltHash.length + saltBytes.length];
System.arraycopy(
pwdPlusSaltHash, 0, digestPlusSalt, 0,
pwdPlusSaltHash.length);
System.arraycopy(
saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length,
saltBytes.length);
// Base64 encode and format string
return Base64.encode(digestPlusSalt);
}
else {
return DigesterUtil.digest(algorithm, clearTextPwd);
}
}
catch (NoSuchAlgorithmException nsae) {
throw new PwdEncryptorException(nsae.getMessage());
}
catch (UnsupportedEncodingException uee) {
throw new PwdEncryptorException(uee.getMessage());
}
}
private static byte[] _getSaltFromCrypt(String cryptString)
throws PwdEncryptorException {
byte[] saltBytes = new byte[2];
try {
if (Validator.isNull(cryptString)) {
// Generate random salt
Random randomGenerator = new Random();
int numSaltChars = saltChars.length;
StringMaker sm = new StringMaker();
int x = Math.abs(randomGenerator.nextInt()) % numSaltChars;
int y = Math.abs(randomGenerator.nextInt()) % numSaltChars;
sm.append(saltChars[x]);
sm.append(saltChars[y]);
String salt = sm.toString();
saltBytes = salt.getBytes(Digester.ENCODING);
}
else {
// Extract salt from encrypted password
String salt = cryptString.substring(0, 3);
saltBytes = salt.getBytes(Digester.ENCODING);
}
}
catch (UnsupportedEncodingException uee) {
throw new PwdEncryptorException(
"Unable to extract salt from encrypted password: " +
uee.getMessage());
}
return saltBytes;
}
private static byte[] _getSaltFromSSHA(String sshaString)
throws PwdEncryptorException {
byte[] saltBytes = new byte[8];
if (Validator.isNull(sshaString)) {
// Generate random salt
Random random = new SecureRandom();
random.nextBytes(saltBytes);
}
else {
// Extract salt from encrypted password
try {
byte[] digestPlusSalt = Base64.decode(sshaString);
byte[] digestBytes = new byte[digestPlusSalt.length - 8];
System.arraycopy(
digestPlusSalt, 0, digestBytes, 0, digestBytes.length);
System.arraycopy(
digestPlusSalt, digestBytes.length, saltBytes, 0,
saltBytes.length);
}
catch (Exception e) {
throw new PwdEncryptorException(
"Unable to extract salt from encrypted password: " +
e.getMessage());
}
}
return saltBytes;
}
}
package com.liferay.portal.util;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.Digester;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <a href="DigesterImpl.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
*
*/
public class DigesterImpl implements Digester {
public String digest(String text) {
return digest(Digester.DIGEST_ALGORITHM, text);
}
public String digest(String algorithm, String text) {
MessageDigest digester = null;
try{
digester = MessageDigest.getInstance(algorithm);
digester.update(text.getBytes(Digester.ENCODING));
}
catch (NoSuchAlgorithmException nsae) {
_log.error(nsae, nsae);
}
catch (UnsupportedEncodingException uee) {
_log.error(uee, uee);
}
byte[] byteArray = digester.digest();
if (_BASE_64) {
return Base64.encode(byteArray);
}
else {
return new String(Hex.encodeHex(byteArray));
}
}
private static final boolean _BASE_64 =
PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
private static Log _log = LogFactory.getLog(Digester.class);
}
package com.liferay.portal.kernel.util;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* <a href="Base64.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
*
*/
public class Base64 {
protected static char getChar(int sixbit) {
if (sixbit >= 0 && sixbit <= 25) {
return (char)(65 + sixbit);
}
if (sixbit >= 26 && sixbit <= 51) {
return (char)(97 + (sixbit - 26));
}
if (sixbit >= 52 && sixbit <= 61) {
return (char)(48 + (sixbit - 52));
}
if (sixbit == 62) {
return '+';
}
return sixbit != 63 ? '?' : '/';
}
protected static int getValue(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 65;
}
if (c >= 'a' && c <= 'z') {
return (c - 97) + 26;
}
if (c >= '0' && c <= '9') {
return (c - 48) + 52;
}
if (c == '+') {
return 62;
}
if (c == '/') {
return 63;
}
return c != '=' ? -1 : 0;
}
public static String encode(byte raw[]) {
StringMaker encoded = new StringMaker();
for (int i = 0; i < raw.length; i += 3) {
encoded.append(encodeBlock(raw, i));
}
return encoded.toString();
}
protected static char[] encodeBlock(byte raw[], int offset) {
int block = 0;
int slack = raw.length - offset - 1;
int end = slack < 2 ? slack : 2;
for (int i = 0; i <= end; i++) {
byte b = raw[offset + i];
int neuter = b >= 0 ? ((int) (b)) : b + 256;
block += neuter << 8 * (2 - i);
}
char base64[] = new char[4];
for (int i = 0; i < 4; i++) {
int sixbit = block >>> 6 * (3 - i) & 0x3f;
base64[i] = getChar(sixbit);
}
if (slack < 1) {
base64[2] = '=';
}
if (slack < 2) {
base64[3] = '=';
}
return base64;
}
public static byte[] decode(String base64) {
int pad = 0;
for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
pad++;
}
int length = (base64.length() * 6) / 8 - pad;
byte raw[] = new byte[length];
int rawindex = 0;
for (int i = 0; i < base64.length(); i += 4) {
int block = (getValue(base64.charAt(i)) << 18) +
(getValue(base64.charAt(i + 1)) << 12) +
(getValue(base64.charAt(i + 2)) << 6) +
getValue(base64.charAt(i + 3));
for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
}
rawindex += 3;
}
return raw;
}
public static String objectToString(Object o) {
if (o == null) {
return null;
}
ByteArrayMaker bam = new ByteArrayMaker(32000);
try {
ObjectOutputStream os = new ObjectOutputStream(
new BufferedOutputStream(bam));
os.flush();
os.writeObject(o);
os.flush();
}
catch (IOException e) {
_log.error(e.getMessage());
}
return encode(bam.toByteArray());
}
public static Object stringToObject(String s) {
if (s == null) {
return null;
}
byte byteArray[] = decode(s);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
try {
ObjectInputStream is =
new ObjectInputStream(new BufferedInputStream(bais));
return is.readObject();
}
catch (Exception e) {
_log.error(e.getMessage());
}
return null;
}
private static Log _log = LogFactoryUtil.getLog(Base64.class);
}
分享到:
相关推荐
Liferay_SSH_开发案例
通过liferay ide 和liferay sdk 6.1实现的插件形式的portlet 集成了liferay和struts2,适合初学者
Liferay Portal Liferay IDE
Liferay系统架构 核心技术 开发Portlet 部署 配置数据库 Liferay的定制 支持多语言
Liferay is a different portal. 10分下载的, 打包5折提供下载. Part 1 Introduction to Liferay 1. Liferay is a different portal 2. Getting started with the Liferay development platform Part 2 Adding ...
这是本人工作了2年有关liferay中的所有技术详细日记 很珍贵
liferay cas 代码
liferay 文档与例子
LifeRay_Portal_6学习笔记6使用Struts2开发Portlet.
liferay的资料-Liferay门户系统技术手册.pdf
打开Liferay IDE,Window-->Preferences-->Server-->Runtime Environment,点击右边Add…,在弹出的服务器选择里面选择Liferay,Inc-->Liferay v6.1 CE (Tomcat 7),请打“Create a new location server”打上勾。...
liferay6+struts2集成项目
liferay-portal-5-2-systems-development-sample-chapter-5-managing-pages[1].pdf LiferayPortal二次开发指南.pdf liferay常用模块的简介.doc Liferay代码研读.pdf Liferay环境搭建.doc
liferay-portal-sql-6.1.1-ce-ga2-20120731132656558
基于Liferay+Portal技术的校园信息门户研究与实现
Liferay之权限的设置管理,介绍大家参考
liferay portal 开发指南liferay portal 开发指南liferay portal 开发指南liferay portal 开发指南
Liferay中的AUI框架,官方叫做Alloy,是一个UI超框架,能为浏览器的三方面,结构,风格和表现建立网络应用提供连续的简单的API。 它融合了三种设计语言:HTML, CSS 和 JAVA。 准确来说AUI是Liferay在YUI的基础上...
liferay 开发技术 框架技术 你值得拥有
liferay 超级学习文档 liferay 框架 java hibernate liferay 不用多说