001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.codec.digest; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertNotNull; 022 023 import java.io.ByteArrayInputStream; 024 import java.io.IOException; 025 import java.security.MessageDigest; 026 import java.util.Random; 027 028 import org.apache.commons.codec.binary.Hex; 029 import org.apache.commons.codec.binary.StringUtils; 030 import org.junit.Test; 031 032 /** 033 * Tests DigestUtils methods. 034 * 035 * @version $Id: DigestUtilsTest.html 889935 2013-12-11 05:05:13Z ggregory $ 036 */ 037 public class DigestUtilsTest { 038 039 private final byte[] testData = new byte[1024*1024]; 040 041 private byte[] getBytesUtf8(String hashMe) { 042 return StringUtils.getBytesUtf8(hashMe); 043 } 044 045 /* (non-Javadoc) 046 * @see junit.framework.TestCase#setUp() 047 */ 048 protected void setUp() throws Exception { 049 new Random().nextBytes(testData); 050 } 051 052 @Test 053 public void testConstructable() { 054 assertNotNull(new DigestUtils()); 055 } 056 057 @Test(expected=IllegalArgumentException.class) 058 public void testInternalNoSuchAlgorithmException() { 059 DigestUtils.getDigest("Bogus Bogus"); 060 } 061 062 @Test 063 public void testMd2Hex() throws IOException { 064 // Examples from RFC 1319 065 assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex("")); 066 067 assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a")); 068 069 assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc")); 070 071 assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest")); 072 073 assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz")); 074 075 assertEquals( 076 "da33def2a42df13975352846c30338cd", 077 DigestUtils.md2Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789")); 078 079 assertEquals( 080 "d5976f79d83d3a0dc9806c3c66f3efd8", 081 DigestUtils.md2Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890")); 082 083 assertEquals(DigestUtils.md2Hex(testData), 084 DigestUtils.md2Hex(new ByteArrayInputStream(testData))); 085 } 086 087 /** 088 * An MD2 hash converted to hex should always be 32 characters. 089 */ 090 @Test 091 public void testMd2HexLength() { 092 String hashMe = "this is some string that is longer than 32 characters"; 093 String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe)); 094 assertEquals(32, hash.length()); 095 096 hashMe = "length < 32"; 097 hash = DigestUtils.md2Hex(getBytesUtf8(hashMe)); 098 assertEquals(32, hash.length()); 099 } 100 101 /** 102 * An MD2 hash should always be a 16 element byte[]. 103 */ 104 @Test 105 public void testMd2Length() { 106 String hashMe = "this is some string that is longer than 16 characters"; 107 byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe)); 108 assertEquals(16, hash.length); 109 110 hashMe = "length < 16"; 111 hash = DigestUtils.md2(getBytesUtf8(hashMe)); 112 assertEquals(16, hash.length); 113 } 114 115 @Test 116 public void testMd5Hex() throws IOException { 117 // Examples from RFC 1321 118 assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex("")); 119 120 assertEquals("0cc175b9c0f1b6a831c399e269772661", DigestUtils.md5Hex("a")); 121 122 assertEquals("900150983cd24fb0d6963f7d28e17f72", DigestUtils.md5Hex("abc")); 123 124 assertEquals("f96b697d7cb7938d525a2f31aaf161d0", DigestUtils.md5Hex("message digest")); 125 126 assertEquals("c3fcd3d76192e4007dfb496cca67e13b", DigestUtils.md5Hex("abcdefghijklmnopqrstuvwxyz")); 127 128 assertEquals( 129 "d174ab98d277d9f5a5611c2c9f419d9f", 130 DigestUtils.md5Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789")); 131 132 assertEquals( 133 "57edf4a22be3c955ac49da2e2107b67a", 134 DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890")); 135 136 assertEquals(DigestUtils.md5Hex(testData), 137 DigestUtils.md5Hex(new ByteArrayInputStream(testData))); 138 } 139 140 /** 141 * An MD5 hash converted to hex should always be 32 characters. 142 */ 143 @Test 144 public void testMd5HexLength() { 145 String hashMe = "this is some string that is longer than 32 characters"; 146 String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); 147 assertEquals(32, hash.length()); 148 149 hashMe = "length < 32"; 150 hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); 151 assertEquals(32, hash.length()); 152 } 153 154 /** 155 * An MD5 hash should always be a 16 element byte[]. 156 */ 157 @Test 158 public void testMd5Length() { 159 String hashMe = "this is some string that is longer than 16 characters"; 160 byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe)); 161 assertEquals(16, hash.length); 162 163 hashMe = "length < 16"; 164 hash = DigestUtils.md5(getBytesUtf8(hashMe)); 165 assertEquals(16, hash.length); 166 } 167 168 @Test 169 public void testSha1Hex() throws IOException { 170 // Examples from FIPS 180-1 171 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc")); 172 173 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc"))); 174 175 assertEquals( 176 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 177 DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); 178 assertEquals(DigestUtils.sha1Hex(testData), 179 DigestUtils.sha1Hex(new ByteArrayInputStream(testData))); 180 } 181 182 @Test 183 public void testSha1UpdateWithByteArray(){ 184 final String d1 = "C'est un homme qui rentre dans un café, et plouf"; 185 final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'"; 186 187 MessageDigest messageDigest = DigestUtils.getSha1Digest(); 188 messageDigest.update(d1.getBytes()); 189 messageDigest.update(d2.getBytes()); 190 final String expectedResult = Hex.encodeHexString(messageDigest.digest()); 191 192 messageDigest = DigestUtils.getSha1Digest(); 193 DigestUtils.updateDigest(messageDigest, d1.getBytes()); 194 DigestUtils.updateDigest(messageDigest, d2.getBytes()); 195 final String actualResult = Hex.encodeHexString(messageDigest.digest()); 196 197 assertEquals(expectedResult, actualResult); 198 } 199 200 @Test 201 public void testSha1UpdateWithString(){ 202 final String d1 = "C'est un homme qui rentre dans un café, et plouf"; 203 final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'"; 204 205 MessageDigest messageDigest = DigestUtils.getSha1Digest(); 206 messageDigest.update(StringUtils.getBytesUtf8(d1)); 207 messageDigest.update(StringUtils.getBytesUtf8(d2)); 208 final String expectedResult = Hex.encodeHexString(messageDigest.digest()); 209 210 messageDigest = DigestUtils.getSha1Digest(); 211 DigestUtils.updateDigest(messageDigest, d1); 212 DigestUtils.updateDigest(messageDigest, d2); 213 final String actualResult = Hex.encodeHexString(messageDigest.digest()); 214 215 assertEquals(expectedResult, actualResult); 216 } 217 218 @Test 219 public void testSha256() throws IOException { 220 // Examples from FIPS 180-2 221 assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", 222 DigestUtils.sha256Hex("abc")); 223 assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", 224 DigestUtils.sha256Hex(getBytesUtf8("abc"))); 225 assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", 226 DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); 227 228 assertEquals(DigestUtils.sha256Hex(testData), 229 DigestUtils.sha256Hex(new ByteArrayInputStream(testData))); 230 } 231 232 @Test 233 public void testSha384() throws IOException { 234 // Examples from FIPS 180-2 235 assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + 236 "8086072ba1e7cc2358baeca134c825a7", 237 DigestUtils.sha384Hex("abc")); 238 assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + 239 "8086072ba1e7cc2358baeca134c825a7", 240 DigestUtils.sha384Hex(getBytesUtf8("abc"))); 241 assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + 242 "fcc7c71a557e2db966c3e9fa91746039", 243 DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + 244 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); 245 assertEquals(DigestUtils.sha384Hex(testData), 246 DigestUtils.sha384Hex(new ByteArrayInputStream(testData))); 247 } 248 249 @Test 250 public void testSha512() throws IOException { 251 // Examples from FIPS 180-2 252 assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + 253 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", 254 DigestUtils.sha512Hex("abc")); 255 assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + 256 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", 257 DigestUtils.sha512Hex(getBytesUtf8("abc"))); 258 assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + 259 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", 260 DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + 261 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); 262 assertEquals(DigestUtils.sha512Hex(testData), 263 DigestUtils.sha512Hex(new ByteArrayInputStream(testData))); 264 } 265 266 @SuppressWarnings("deprecation") // deliberate tests of deprecated code 267 @Test 268 public void testShaHex() throws IOException { 269 // Examples from FIPS 180-1 270 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); 271 272 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc"))); 273 274 assertEquals( 275 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 276 DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); 277 assertEquals(DigestUtils.shaHex(testData), 278 DigestUtils.shaHex(new ByteArrayInputStream(testData))); 279 } 280 281 @SuppressWarnings("deprecation") // deliberate tests of deprecated code 282 @Test 283 public void testShaUpdateWithByteArray(){ 284 final String d1 = "C'est un homme qui rentre dans un café, et plouf"; 285 final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'"; 286 287 MessageDigest messageDigest = DigestUtils.getShaDigest(); 288 messageDigest.update(d1.getBytes()); 289 messageDigest.update(d2.getBytes()); 290 final String expectedResult = Hex.encodeHexString(messageDigest.digest()); 291 292 messageDigest = DigestUtils.getShaDigest(); 293 DigestUtils.updateDigest(messageDigest, d1.getBytes()); 294 DigestUtils.updateDigest(messageDigest, d2.getBytes()); 295 final String actualResult = Hex.encodeHexString(messageDigest.digest()); 296 297 assertEquals(expectedResult, actualResult); 298 } 299 300 @SuppressWarnings("deprecation") // deliberate tests of deprecated code 301 @Test 302 public void testShaUpdateWithString(){ 303 final String d1 = "C'est un homme qui rentre dans un café, et plouf"; 304 final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau: 'oh... encore un chapeau!'"; 305 306 MessageDigest messageDigest = DigestUtils.getShaDigest(); 307 messageDigest.update(StringUtils.getBytesUtf8(d1)); 308 messageDigest.update(StringUtils.getBytesUtf8(d2)); 309 final String expectedResult = Hex.encodeHexString(messageDigest.digest()); 310 311 messageDigest = DigestUtils.getShaDigest(); 312 DigestUtils.updateDigest(messageDigest, d1); 313 DigestUtils.updateDigest(messageDigest, d2); 314 final String actualResult = Hex.encodeHexString(messageDigest.digest()); 315 316 assertEquals(expectedResult, actualResult); 317 } 318 319 }