defaultpasswordencoder.java

来自「CAS在Tomcat中实现单点登录项目,单点登录(Single Sign On 」· Java 代码 · 共 69 行

JAVA
69
字号
/* * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license * distributed with this file and available online at * http://www.uportal.org/license.html */package org.jasig.cas.authentication.handler;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import org.jasig.cas.util.annotation.NotNull;/** * Implementation of PasswordEncoder using message digest. Can accept any * message digest that the JDK can accept, including MD5 and SHA1. Returns the * equivalent Hash you would get from a Perl digest. *  * @author Scott Battaglia * @author Stephen More * @version $Revision: 42053 $ $Date: 2007-06-10 09:17:55 -0400 (Sun, 10 Jun 2007) $ * @since 3.1 */public final class DefaultPasswordEncoder implements PasswordEncoder {    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',        '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};    @NotNull    private final String encodingAlgorithm;    public DefaultPasswordEncoder(final String encodingAlgorithm) {        this.encodingAlgorithm = encodingAlgorithm;    }    public String encode(final String password) {        if (password == null) {            return null;        }        try {            MessageDigest messageDigest = MessageDigest                .getInstance(this.encodingAlgorithm);            messageDigest.update(password.getBytes());            final byte[] digest = messageDigest.digest();            return getFormattedText(digest);        } catch (NoSuchAlgorithmException e) {            throw new SecurityException(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(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();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?