📄 encryptutils.java
字号:
// $Id: EncryptUtils.java 306725 2005-09-09 16:55:35 +0530 (Fri, 09 Sep 2005) rana_b $
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ftpserver.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* String encryption utility methods.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
class EncryptUtils {
/**
* Encrypt byte array.
*/
public final static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.reset();
md.update(source);
return md.digest();
}
/**
* Encrypt string
*/
public final static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException {
byte[] resByteArray = encrypt(source.getBytes(), algorithm);
return StringUtils.toHexString(resByteArray);
}
/**
* Encrypt string using MD5 algorithm
*/
public final static String encryptMD5(String source) {
if (source == null) {
source = "";
}
String result = "";
try {
result = encrypt(source, "MD5");
}
catch(NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return result;
}
/**
* Encrypt string using SHA algorithm
*/
public final static String encryptSHA(String source) {
if (source == null) {
source = "";
}
String result = "";
try {
result = encrypt(source, "SHA");
}
catch(NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -