apache的MD5算法,apacheMD5算法
apache的MD5算法,apacheMD5算法
018 |
package org.apache.commons.codec.digest; |
019 |
020 |
import java.io.ByteArrayInputStream; |
021 |
import java.io.IOException; |
022 |
import java.util.Random; |
023 |
024 |
import org.apache.commons.codec.binary.StringUtils; |
025 |
026 |
import junit.framework.TestCase; |
027 |
028 |
/*** |
029 |
*
Tests DigestUtils methods. |
030 |
* |
031 |
*
@author Apache Software Foundation |
032 |
*
@version $Id: DigestUtilsTest.java 800270 2009-08-03 09:32:08Z ggregory $ |
033 |
*/ |
034 |
public class DigestUtilsTest extends TestCase
{ |
035 |
036 |
private byte []
testData = new byte [ 1024 * 1024 ]; |
037 |
|
038 |
private byte []
getBytesUtf8(String hashMe) { |
039 |
return StringUtils.getBytesUtf8(hashMe); |
040 |
} |
041 |
042 |
/**
(non-Javadoc) |
043 |
*
@see junit.framework.TestCase#setUp() |
044 |
*/ |
045 |
protected void setUp() throws Exception
{ |
046 |
new Random().nextBytes(testData); |
047 |
} |
048 |
049 |
public void testConstructable()
{ |
050 |
new DigestUtils(); |
051 |
} |
052 |
|
053 |
public void testInternalNoSuchAlgorithmException()
{ |
054 |
try { |
055 |
DigestUtils.getDigest( "Bogus
Bogus" ); |
056 |
fail( "A
RuntimeException should have been thrown." ); |
057 |
} catch (RuntimeException
e) { |
058 |
//
Expected exception. |
059 |
} |
060 |
} |
061 |
062 |
public void testMd5Hex() throws IOException
{ |
063 |
//
Examples from RFC 1321 |
064 |
assertEquals( "d41d8cd98f00b204e9800998ecf8427e" ,
DigestUtils.md5Hex( "" )); |
065 |
066 |
assertEquals( "0cc175b9c0f1b6a831c399e269772661" ,
DigestUtils.md5Hex( "a" )); |
067 |
068 |
assertEquals( "900150983cd24fb0d6963f7d28e17f72" ,
DigestUtils.md5Hex( "abc" )); |
069 |
070 |
assertEquals( "f96b697d7cb7938d525a2f31aaf161d0" ,
DigestUtils.md5Hex( "message
digest" )); |
071 |
072 |
assertEquals( "c3fcd3d76192e4007dfb496cca67e13b" ,
DigestUtils.md5Hex( "abcdefghijklmnopqrstuvwxyz" )); |
073 |
074 |
assertEquals( |
075 |
"d174ab98d277d9f5a5611c2c9f419d9f" , |
076 |
DigestUtils.md5Hex( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" )); |
077 |
078 |
assertEquals( |
079 |
"57edf4a22be3c955ac49da2e2107b67a" , |
080 |
DigestUtils.md5Hex( "1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890" )); |
081 |
082 |
assertEquals(DigestUtils.md5Hex(testData), |
083 |
DigestUtils.md5Hex( new ByteArrayInputStream(testData))); |
084 |
} |
085 |
086 |
/*** |
087 |
*
An MD5 hash converted to hex should always be 32 characters. |
088 |
*/ |
089 |
public void testMD5HexLength()
{ |
090 |
String
hashMe = "this
is some string that is longer than 32 characters" ; |
091 |
String
hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); |
092 |
assertEquals( 32 ,
hash.length()); |
093 |
094 |
hashMe
= "length
< 32" ; |
095 |
hash
= DigestUtils.md5Hex(getBytesUtf8(hashMe)); |
096 |
assertEquals( 32 ,
hash.length()); |
097 |
} |
098 |
099 |
/*** |
100 |
*
An MD5 hash should always be a 16 element byte[]. |
101 |
*/ |
102 |
public void testMD5Length()
{ |
103 |
String
hashMe = "this
is some string that is longer than 16 characters" ; |
104 |
byte []
hash = DigestUtils.md5(getBytesUtf8(hashMe)); |
105 |
assertEquals( 16 ,
hash.length); |
106 |
107 |
hashMe
= "length
< 16" ; |
108 |
hash
= DigestUtils.md5(getBytesUtf8(hashMe)); |
109 |
assertEquals( 16 ,
hash.length); |
110 |
} |
111 |
112 |
public void testSha256() throws IOException
{ |
113 |
//
Examples from FIPS 180-2 |
114 |
assertEquals( "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" , |
115 |
DigestUtils.sha256Hex( "abc" )); |
116 |
assertEquals( "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" , |
117 |
DigestUtils.sha256Hex(getBytesUtf8( "abc" ))); |
118 |
assertEquals( "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" , |
119 |
DigestUtils.sha256Hex( "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" )); |
120 |
|
121 |
assertEquals(DigestUtils.sha256Hex(testData), |
122 |
DigestUtils.sha256Hex( new ByteArrayInputStream(testData))); |
123 |
} |
124 |
125 |
public void testSha384() throws IOException
{ |
126 |
//
Examples from FIPS 180-2 |
127 |
assertEquals( "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + |
128 |
"8086072ba1e7cc2358baeca134c825a7" , |
129 |
DigestUtils.sha384Hex( "abc" )); |
130 |
assertEquals( "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + |
131 |
"8086072ba1e7cc2358baeca134c825a7" , |
132 |
DigestUtils.sha384Hex(getBytesUtf8( "abc" ))); |
133 |
assertEquals( "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + |
134 |
"fcc7c71a557e2db966c3e9fa91746039" , |
135 |
DigestUtils.sha384Hex( "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + |
136 |
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" )); |
137 |
assertEquals(DigestUtils.sha384Hex(testData), |
138 |
DigestUtils.sha384Hex( new ByteArrayInputStream(testData))); |
139 |
} |
140 |
141 |
public void testSha512() throws IOException
{ |
142 |
//
Examples from FIPS 180-2 |
143 |
assertEquals( "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + |
144 |
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" , |
145 |
DigestUtils.sha512Hex( "abc" )); |
146 |
assertEquals( "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + |
147 |
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" , |
148 |
DigestUtils.sha512Hex(getBytesUtf8( "abc" ))); |
149 |
assertEquals( "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + |
150 |
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" , |
151 |
DigestUtils.sha512Hex( "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + |
152 |
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" )); |
153 |
assertEquals(DigestUtils.sha512Hex(testData), |
154 |
DigestUtils.sha512Hex( new ByteArrayInputStream(testData))); |
155 |
} |
156 |
157 |
public void testShaHex() throws IOException
{ |
158 |
//
Examples from FIPS 180-1 |
159 |
assertEquals( "a9993e364706816aba3e25717850c26c9cd0d89d" ,
DigestUtils.shaHex( "abc" )); |
160 |
161 |
assertEquals( "a9993e364706816aba3e25717850c26c9cd0d89d" ,
DigestUtils.shaHex(getBytesUtf8( "abc" ))); |
162 |
163 |
assertEquals( |
164 |
"84983e441c3bd26ebaae4aa1f95129e5e54670f1" , |
165 |
DigestUtils.shaHex( "abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq" )); |
166 |
assertEquals(DigestUtils.shaHex(testData), |
167 |
DigestUtils.shaHex( new ByteArrayInputStream(testData))); |
168 |
169 |
} |
评论暂时关闭