Apache Commons Codec 与消息摘要算法(hash算法)
首先我们要明白 Codec 是什么含义。它是 Coder + decoder = Codec,也就是编码器解码器。即是编码器,也是解码器。
官网地址:http://commons.apache.org/proper/commons-codec/
Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.
主要提供的是 Base64, Hex, Phonetic and URLs 等的编码和解密。
Impetus
Codec was formed as an attempt to focus development effort on one definitive implementation of the Base64 encoder. At the time of Codec's proposal, there were approximately 34 different Java classes that dealt with Base64 encoding spread over the Foundation's CVS repository. Developers in the Jakarta Tomcat project had implemented an original version of the Base64 codec which had been copied by the Commons HttpClient and Apache XML project's XML-RPC subproject. After almost one year, the two forked versions of Base64 had significantly diverged from one another. XML-RPC had applied numerous fixes and patches which were not applied to the Commons HttpClient Base64. Different subprojects had differing implementations at various levels of compliance with the RFC 2045.
Out of that confusing duplication of effort sprang this simple attempt to encourage code reuse among various projects. While this package contains a abstract framework for the creation of encoders and decoders, Codec itself is primarily focused on providing functional utilities for working with common encodings.
建立项目的原因是:促进 Base64 等编码算法的标准化,统一化。因为 Base64 有很多不同的实现,互不兼容。
其实我们使用 Apache Commons Codec,主要原因并不是使用它的 编码解码的功能,一般我们是看中它实现了很完整的“消息摘要”算法,也称hash算法。
Hex,Base64等待编码解码功能只是辅助于“消息摘要”算法而已。
消息摘要算法的实现主要是其中的:DigestUtils
static MessageDigest getDigest(String algorithm) Returns a MessageDigest for the given algorithm.
static MessageDigest getMd2Digest() Returns an MD2 MessageDigest.
static MessageDigest getMd5Digest() Returns an MD5 MessageDigest.
static MessageDigest getSha1Digest() Returns an SHA-1 digest.
static MessageDigest getSha256Digest() Returns an SHA-256 digest.
static MessageDigest getSha384Digest() Returns an SHA-384 digest.
static MessageDigest getSha512Digest() Returns an SHA-512 digest.
static byte[] md2(byte[] data) Calculates the MD2 digest and returns the value as a 16 element byte[].
static byte[] md2(InputStream data) Calculates the MD2 digest and returns the value as a 16 element byte[].
static byte[] md2(String data) Calculates the MD2 digest and returns the value as a 16 element byte[].
static String md2Hex(byte[] data) Calculates the MD2 digest and returns the value as a 32 character hex string.
static String md2Hex(InputStream data) Calculates the MD2 digest and returns the value as a 32 character hex string.
static String md2Hex(String data) Calculates the MD2 digest and returns the value as a 32 character hex string.
static byte[] md5(byte[] data) Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[] md5(InputStream data) Calculates the MD5 digest and returns the value as a 16 element byte[].
static byte[] md5(String data) Calculates the MD5 digest and returns the value as a 16 element byte[].
static String md5Hex(byte[] data) Calculates the MD5 digest and returns the value as a 32 character hex string.
static String md5Hex(InputStream data) Calculates the MD5 digest and returns the value as a 32 character hex string.
static String md5Hex(String data) Calculates the MD5 digest and returns the value as a 32 character hex string.
static byte[] sha1(byte[] data) Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha1(InputStream data) Calculates the SHA-1 digest and returns the value as a byte[].
static byte[] sha1(String data) Calculates the SHA-1 digest and returns the value as a byte[].
static String sha1Hex(byte[] data) Calculates the SHA-1 digest and returns the value as a hex string.
static String sha1Hex(InputStream data) Calculates the SHA-1 digest and returns the value as a hex string.
static String sha1Hex(String data) Calculates the SHA-1 digest and returns the value as a hex string.
static byte[] sha256(byte[] data) Calculates the SHA-256 digest and returns the value as a byte[].
static byte[] sha256(InputStream data) Calculates the SHA-256 digest and returns the value as a byte[].
static byte[] sha256(String data) Calculates the SHA-256 digest and returns the value as a byte[].
static String sha256Hex(byte[] data) Calculates the SHA-256 digest and returns the value as a hex string.
static String sha256Hex(InputStream data) Calculates the SHA-256 digest and returns the value as a hex string.
static String sha256Hex(String data) Calculates the SHA-256 digest and returns the value as a hex string.
static byte[] sha384(byte[] data) Calculates the SHA-384 digest and returns the value as a byte[].
static byte[] sha384(InputStream data) Calculates the SHA-384 digest and returns the value as a byte[].
static byte[] sha384(String data) Calculates the SHA-384 digest and returns the value as a byte[].
static String sha384Hex(byte[] data) Calculates the SHA-384 digest and returns the value as a hex string.
static String sha384Hex(InputStream data) Calculates the SHA-384 digest and returns the value as a hex string.
static String sha384Hex(String data) Calculates the SHA-384 digest and returns the value as a hex string.
static byte[] sha512(byte[] data) Calculates the SHA-512 digest and returns the value as a byte[].
static byte[] sha512(InputStream data) Calculates the SHA-512 digest and returns the value as a byte[].
static byte[] sha512(String data) Calculates the SHA-512 digest and returns the value as a byte[].
static String sha512Hex(byte[] data) Calculates the SHA-512 digest and returns the value as a hex string.
static String sha512Hex(InputStream data) Calculates the SHA-512 digest and returns the value as a hex string.
static String sha512Hex(String data) Calculates the SHA-512 digest and returns the value as a hex string. static MessageDigest updateDigest(MessageDigest messageDigest, byte[] valueToDigest) Updates the given MessageDigest.
static MessageDigest updateDigest(MessageDigest digest, InputStream data) Reads through an InputStream and updates the digest for the data
static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) Updates the given MessageDigest.
针对上面怎么多和“摘要算法”相关的函数,其实他们的存在是为了方便使用。
我们看到这些函数的参数分为了三种:byte[], InputStream, String,为了方便使用。消息摘要算法分为了 MD 系列和 SHA系列。
其实区分他们的最重要的方法,是看他们的返回值类型:
1)如果返回类型为 byte[],那么他们是 加密函数,或者说Hash. 也就是 Calculates the digest,计算摘要的功能。
2)如果返回类型是 String, 那么他们是 既包含了加密的过程,也就是计算摘要的过程,同时还将计算的结果转换成16进制编码的String,以利于存储结果和比较结果。一步到位。
3)返回MessageDigest的函数有两类,他们都是为了实现:加盐的多次迭代的“消息摘要”算法。下面看一个例子:
import java.security.MessageDigest; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.binary.Hex;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.junit.Test; public class CodecTest {
@Test
public void test(){
MessageDigest digest = DigestUtils.getSha256Digest();
digest.update("salt".getBytes());
byte[] rs = digest.digest("just a test".getBytes());
int iterations = 10 - 1;
for (int i = 0; i < iterations; i++) {
digest.reset();
rs = digest.digest(rs);
} System.out.println(Hex.encodeHex(rs));
System.out.println(new SimpleHash("sha-256", "just a test", "salt", 10).toString()); MessageDigest digest2 = DigestUtils.getSha256Digest();
DigestUtils.updateDigest(digest2, "salt");
byte[] rs2 = digest2.digest("just a test".getBytes());
for (int i = 0; i < 9; i++) {
digest2.reset();
rs2 = digest2.digest(rs2);
}
System.out.println(Hex.encodeHex(rs2));
}
}
输出结果:
8cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead49
8cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead49
8cfa3262a7dd6af87f9c60fabd56eafd2d3861164b86b0afa6c103fed63ead49
上面使用了三种方法来实现:对字符串"just a test" 利于算法 "sha-256",盐为"salt",迭代次数为10,的算法。也演示了 Apache Commons Codec 中DigestUtils 和 Hex 的用法。
关于为什么要进行 Hex 16进制编码及相关问题,参见 加密解密基础问题:字节数组和字符串的相互转换
Hex 类在 16进制字符串和byte[]以及原始String的转换:
@Test
public void testHex() throws DecoderException{
String str = "hello, 世界。";
char[] c = Hex.encodeHex(str.getBytes());
String hexStr = new String(c);
System.out.println(hexStr); hexStr = Hex.encodeHexString(str.getBytes());
System.out.println(hexStr); String originalStr = new String(Hex.decodeHex(hexStr.toCharArray()));
System.out.println(originalStr);
}
结果:
68656c6c6f2c20e4b896e7958ce38082
68656c6c6f2c20e4b896e7958ce38082
hello, 世界。
Apache Commons Codec 与消息摘要算法(hash算法)的更多相关文章
- Apache Commons Codec 编码解码
Apache Commons Codec jar包官方下载地址 下载解压后把commons-codec-1.9.jar 放到lib中 关于SHA1算法的介绍可以参看Wiki:http://en.wik ...
- Java之加密(信息摘要)工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)
依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256 import java.security.MessageDigest; import java.s ...
- ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android
Andriod系统包中现在已经自带加密函数,如果用apache的codec包则会报以上错误,用android.util.Base64以下方法代替org.apache.commons.codec.bin ...
- Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.codec.digest.DigestUtils.sha1Hex(Ljava/lang/String;)Ljava/lang/String;
异常:Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.co ...
- Apache Commons Codec的Base64加解密库
下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi import org.apache.commons.cod ...
- java 调用apache.commons.codec的包简单实现MD5加密
转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...
- md5加密(3)---org.apache.commons.codec.digest.DigestUtils.md5Hex(input)
import org.apache.commons.codec.digest.DigestUtils;String sig = DigestUtils.md5Hex("str")
- 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在
import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...
- import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Base64;
随机推荐
- Windows10系统远程桌面连接出现卡顿如何解决
最新的windows10系统下,用户只要开启远程桌面连接,就能够轻松地操控其他电脑.但是,最近部分用户在win10中启用远程连接时,发现电脑窗口变得非常缓慢卡顿,这是怎么回事呢?其实,该问题与系统的设 ...
- 解决com.mongodb.MongoException$CursorNotFound: cursor 0 not found on server
背景 经常需要执行脚本调用Java程序读取mongodb中数据,本来是转为后台进程.偶尔看看日志的简单任务.今天发现程序抛出异常“com.mongodb.MongoException$CursorNo ...
- 如何用js创建表格?
1.用js创建表格 <script> function createTable(){ //创建表格 //创建对象 //window下面的属性方法可以把window去掉或者写上 var ta ...
- JAVA消息 JMS 很重要
首先大致讲一下,java 消息模块 消息,个人理解分为两种:1.同步消息(RPC调用) 2.异步消息(本篇讲解部分) 一.同步消息java提供了多种方案: 最新比较常用的方式就是spring Http ...
- 2-7-搭建DNS服务器实现域名解析
学习服务的方法: 了解服务的作用:名称,功能,特点 安装服务 配置文件的位置,端口 服务开启和关闭的脚本 修改配置文件(实战举例) 排错(从上到下,从内到外) -------------------- ...
- cdq分治的小结
cdq分治 是一种特殊的分治 他的思想: 1.分治l,mid 2.分治mid+1,r 3.计算l,mid对mid+1,r的影响 3就是最关键的地方 这也是cdq的关键点 想到了这一步基本就可以做了 接 ...
- ISE创建Microblaze软核(二)
ISE创建Microblaze软核(二) (2012-07-13 15:09:08) 转载▼ 标签: 杂谈 分类: FPGA开发 第四步 进入Platform Studio操作界面 通过向导创建软核后 ...
- Facebook的工程师文化——《打造facebook》读后感
在今年北京的QCon大会上听了facebook早期中国籍工程师王淮的演讲,受益匪浅,主题是如何打造高效能团队,主要介绍他在facebook的一些经历和管理上的经验分享.现在的他是一名天使投资人,投资的 ...
- eclipse SVN 上传.so库文件
eclipse SVN提交代码的时候,是自动忽略.so库文件的.用下面所说的操作后,.so库文件右下角的图标会变成一个蓝色的+号的图标,这样就可以提交.so文件了 选择要上传的.so文件,右键 ——& ...
- 20181009-6 选题 Scrum立会报告+燃尽图 05
Scrum立会报告+燃尽图(05)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2195 一.小组介绍 组长:刘莹莹 ...