com.feilong.security.oneway.SHA256Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* 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 com.feilong.security.oneway;
import com.feilong.core.CharsetType;
import com.feilong.security.EncryptionException;
/**
* The Class SHA256Util.
*
* @author feilong
* @see OnewayType#SHA256
* @since 1.14.2
*/
public final class SHA256Util{
/** The oneway type. */
private static final OnewayType ONEWAYTYPE = OnewayType.SHA256;
//---------------------------------------------------------------
/** Don't let anyone instantiate this class. */
private SHA256Util(){
//AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
//see 《Effective Java》 2nd
throw new AssertionError("No " + getClass().getName() + " instances for you!");
}
//---------------------------------------------------------------
/**
* 使用sha256算法 单向加密字符串.
*
*
* 加密之后的转成小写的16进制字符串
*
*
* @param origin
* 原始字符串,将使用默认的 {@link String#getBytes()} 转成字节数组
* @return 加密之后的转成小写的16进制字符串
* @throws EncryptionException
* 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出
* @see OnewayEncryption#encode(OnewayType, String)
* @see com.feilong.lib.codec.digest.DigestUtils#sha256Hex(String)
*/
public static String encode(String origin){
return OnewayEncryption.encode(ONEWAYTYPE, origin);
}
/**
* 使用sha256算法 单向加密字符串.
*
*
* 加密之后的转成小写的16进制字符串
*
*
* @param origin
* 原始字符串,将使用默认的 value.getBytes() 转成字节数组
* 如果需要string 转码,请自行调用value.getBytes(string chartsetname),再调用{@link #encode(String, String)}
* @param charsetName
* 受支持的 {@link CharsetType} 名称,比如 utf-8
* @return 加密之后的转成小写的16进制字符串
* @throws EncryptionException
* 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出
* @see OnewayEncryption#encode(OnewayType, String, String)
* @see com.feilong.lib.codec.digest.DigestUtils#sha256(byte[])
*/
public static String encode(String origin,String charsetName){
return OnewayEncryption.encode(ONEWAYTYPE, origin, charsetName);
}
/**
* 使用sha256算法 单向加密 inputBytes.
*
*
* 加密之后的转成小写的16进制,长度32位的字符串
*
*
* @param inputBytes
* the input bytes
* @return 加密之后的转成 小写的16进制字符串
* @see OnewayEncryption#encode(OnewayType, byte[])
* @since 3.4.0
*/
public static String encode(byte[] inputBytes){
return OnewayEncryption.encode(ONEWAYTYPE, inputBytes);
}
//---------------------------------------------------------------
/**
* 计算文件的单向加密值.
*
* @param location
*
* - 支持全路径, 比如. "file:C:/test.dat".
* - 支持classpath 伪路径, e.g. "classpath:test.dat".
* - 支持相对路径, e.g. "WEB-INF/test.dat".
* - 如果上述都找不到,会再次转成FileInputStream,比如 "/Users/feilong/feilong-io/src/test/resources/readFileToString.txt"
*
* @return 如果 location
是null,抛出 {@link NullPointerException}
* 如果 location
是blank,抛出 {@link IllegalArgumentException}
* @throws EncryptionException
* 如果在加密解密的过程中发生了异常,会以EncryptionException形式抛出
* @see OnewayEncryption#encodeFile(OnewayType, String)
* @see com.feilong.lib.codec.digest.DigestUtils#sha256Hex(java.io.InputStream)
*/
public static String encodeFile(String location){
return OnewayEncryption.encodeFile(ONEWAYTYPE, location);
}
}