Commons Codec - 常见的编码解码

Base64

Base64 编码

assertEquals("T3chIQ==", Base64.encodeBase64String("Ow!!".getBytes()));

安全 URL 的 Base64 编码

assertEquals("T3chIQ", Base64.encodeBase64URLSafeString("Ow!!".getBytes()));

Base64 解码

assertEquals("Ow!!", new String(Base64.decodeBase64("T3chIQ==")));
assertEquals("Ow!!", new String(Base64.decodeBase64("T3chIQ")));

 

URL

URL 编码

URLCodec urlCodec = new URLCodec();
assertEquals("c%2B%2B", urlCodec.encode("c++"));

URL 解码

URLCodec urlCodec = new URLCodec();
assertEquals("c++", urlCodec.decode("c%2B%2B"));

 

MD5

MD5 编码

assertEquals("5d41402abc4b2a76b9719d911017c592", DigestUtils.md5Hex("hello").toLowerCase());

 

SHA1

SHA1 编码

assertEquals("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", DigestUtils.sha1Hex("hello"));

 

SHA256

SHA256 编码

assertEquals("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", DigestUtils.sha256Hex("hello"));

 

posted on 2016-04-22 16:49  huey2672  阅读(878)  评论(0编辑  收藏  举报