首页>代码>eclipse maven开发CAS SSO改造后的java web程序>/mysso/src/test/java/com/demo/sso/extension/UserPasswordEncoder.java
/**************************************************************************************** 
 Copyright 漏 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.demo.sso.extension;

import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;

import org.jasig.cas.authentication.handler.PasswordEncoder;

/** 
 * <Description> <br> 
 *  
 * @author XXX<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014骞�0鏈�0鏃�<br>
 * @since V1.0<br>
 * @see com.ztesoft.uboss.sso.extension <br>
 */

public class UserPasswordEncoder implements PasswordEncoder {
    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
        '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private String characterEncoding;
    
    /**
     * Description: <br> 
     *  
     * @author XXX<br>
     * @taskId <br>
     * @param password
     * @return <br>
     */
    public String encode(String password) {
        if (password == null) {
            return null;
        }
        
        try {
            String base64Str = (new BASE64Encoder()).encode(password.getBytes());
            
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        
            if (StringUtils.hasText(this.characterEncoding)) {
                messageDigest.update(base64Str.getBytes(this.characterEncoding));
            } else {
                messageDigest.update(base64Str.getBytes());
            }


            final byte[] digest = messageDigest.digest();

            return getFormattedText(digest);
        
        } catch (final NoSuchAlgorithmException e) {
            throw new SecurityException(e);
        } catch (final UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * Takes the raw bytes from the digest and formats them correct.
     *
     * @param bytes the raw bytes from the digest.
     * @return the formatted bytes.
     */
    private String getFormattedText(final byte[] bytes) {
        final StringBuilder buf = new StringBuilder(bytes.length * 2);

        for (int j = 0; j < bytes.length; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }

    public void setCharacterEncoding(final String characterEncoding) {
        this.characterEncoding = characterEncoding;
    }
    
    public static void main(String args[]){
        UserPasswordEncoder s = new UserPasswordEncoder();
        System.out.println(s.encode("111111"));  //e58521190f0e6c8a0f1c4a5294ff1307
    }
}
最近下载更多
lcqlcl  LV11 2023年8月29日
bai620123  LV16 2020年11月29日
zhen_cd  LV10 2020年11月14日
李海洋  LV12 2020年11月2日
杠上炮  LV6 2020年7月22日
C544350851  LV26 2020年6月17日
lyd19931203  LV21 2020年4月20日
13086871847  LV4 2020年1月14日
fat man  LV10 2020年1月10日
553152469  LV3 2019年11月17日
最近浏览更多
lcqlcl  LV11 2023年8月29日
徐万强  LV9 2023年7月25日
wjh12345654321  LV14 2022年9月14日
最代码-宋家辉  LV61 2022年8月22日
qwqw900619  LV4 2022年7月28日
钱钱11111  LV2 2022年6月18日
舒舒不吃葱花  LV2 2022年6月12日
skook7  LV2 2022年5月16日
zackery  LV9 2022年4月19日
zhaoyangwfd  LV17 2022年3月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友