Skip to content

Commit 25ad9f8

Browse files
committed
[CODEC-73] DigestUtils: Make string2byte conversions indepedent of platform default encoding.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@800270 13f79535-47bb-0310-9956-ffa450edef68
1 parent cc6df03 commit 25ad9f8

2 files changed

Lines changed: 70 additions & 39 deletions

File tree

src/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.NoSuchAlgorithmException;
2424

2525
import org.apache.commons.codec.binary.Hex;
26+
import org.apache.commons.codec.binary.StringUtils;
2627

2728
/**
2829
* Operations to simplifiy common {@link java.security.MessageDigest} tasks. This class is thread safe.
@@ -37,23 +38,37 @@ public class DigestUtils {
3738
/**
3839
* Read through an InputStream and returns the digest for the data
3940
*
40-
* @param digest The MessageDigest to use (e.g. MD5)
41-
* @param data Data to digest
41+
* @param digest
42+
* The MessageDigest to use (e.g. MD5)
43+
* @param data
44+
* Data to digest
4245
* @return MD5 digest
43-
* @throws IOException On error reading from the stream
46+
* @throws IOException
47+
* On error reading from the stream
4448
*/
4549
private static byte[] digest(MessageDigest digest, InputStream data) throws IOException {
4650
byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
4751
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
48-
49-
while(read > -1) {
52+
53+
while (read > -1) {
5054
digest.update(buffer, 0, read);
5155
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
5256
}
53-
57+
5458
return digest.digest();
5559
}
5660

61+
/**
62+
* Calls {@link StringUtils#getBytesUtf8(String)}
63+
*
64+
* @param string
65+
* the String to encode
66+
* @return encoded bytes
67+
*/
68+
private static byte[] getBytesUtf8(String data) {
69+
return StringUtils.getBytesUtf8(data);
70+
}
71+
5772
/**
5873
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
5974
*
@@ -138,7 +153,7 @@ private static MessageDigest getSha512Digest() {
138153
private static MessageDigest getShaDigest() {
139154
return getDigest("SHA");
140155
}
141-
156+
142157
/**
143158
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
144159
*
@@ -156,7 +171,8 @@ public static byte[] md5(byte[] data) {
156171
* @param data
157172
* Data to digest
158173
* @return MD5 digest
159-
* @throws IOException On error reading from the stream
174+
* @throws IOException
175+
* On error reading from the stream
160176
* @since 1.4
161177
*/
162178
public static byte[] md5(InputStream data) throws IOException {
@@ -171,7 +187,7 @@ public static byte[] md5(InputStream data) throws IOException {
171187
* @return MD5 digest
172188
*/
173189
public static byte[] md5(String data) {
174-
return md5(data.getBytes());
190+
return md5(getBytesUtf8(data));
175191
}
176192

177193
/**
@@ -191,7 +207,8 @@ public static String md5Hex(byte[] data) {
191207
* @param data
192208
* Data to digest
193209
* @return MD5 digest as a hex string
194-
* @throws IOException On error reading from the stream
210+
* @throws IOException
211+
* On error reading from the stream
195212
* @since 1.4
196213
*/
197214
public static String md5Hex(InputStream data) throws IOException {
@@ -226,7 +243,8 @@ public static byte[] sha(byte[] data) {
226243
* @param data
227244
* Data to digest
228245
* @return SHA-1 digest
229-
* @throws IOException On error reading from the stream
246+
* @throws IOException
247+
* On error reading from the stream
230248
*/
231249
public static byte[] sha(InputStream data) throws IOException {
232250
return digest(getShaDigest(), data);
@@ -240,7 +258,7 @@ public static byte[] sha(InputStream data) throws IOException {
240258
* @return SHA-1 digest
241259
*/
242260
public static byte[] sha(String data) {
243-
return sha(data.getBytes());
261+
return sha(getBytesUtf8(data));
244262
}
245263

246264
/**
@@ -267,7 +285,8 @@ public static byte[] sha256(byte[] data) {
267285
* @param data
268286
* Data to digest
269287
* @return SHA-256 digest
270-
* @throws IOException On error reading from the stream
288+
* @throws IOException
289+
* On error reading from the stream
271290
* @since 1.4
272291
*/
273292
public static byte[] sha256(InputStream data) throws IOException {
@@ -286,7 +305,7 @@ public static byte[] sha256(InputStream data) throws IOException {
286305
* @since 1.4
287306
*/
288307
public static byte[] sha256(String data) {
289-
return sha256(data.getBytes());
308+
return sha256(getBytesUtf8(data));
290309
}
291310

292311
/**
@@ -313,7 +332,8 @@ public static String sha256Hex(byte[] data) {
313332
* @param data
314333
* Data to digest
315334
* @return SHA-256 digest as a hex string
316-
* @throws IOException On error reading from the stream
335+
* @throws IOException
336+
* On error reading from the stream
317337
* @since 1.4
318338
*/
319339
public static String sha256Hex(InputStream data) throws IOException {
@@ -359,13 +379,14 @@ public static byte[] sha384(byte[] data) {
359379
* @param data
360380
* Data to digest
361381
* @return SHA-384 digest
362-
* @throws IOException On error reading from the stream
382+
* @throws IOException
383+
* On error reading from the stream
363384
* @since 1.4
364385
*/
365386
public static byte[] sha384(InputStream data) throws IOException {
366387
return digest(getSha384Digest(), data);
367388
}
368-
389+
369390
/**
370391
* Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
371392
* <p>
@@ -378,7 +399,7 @@ public static byte[] sha384(InputStream data) throws IOException {
378399
* @since 1.4
379400
*/
380401
public static byte[] sha384(String data) {
381-
return sha384(data.getBytes());
402+
return sha384(getBytesUtf8(data));
382403
}
383404

384405
/**
@@ -405,7 +426,8 @@ public static String sha384Hex(byte[] data) {
405426
* @param data
406427
* Data to digest
407428
* @return SHA-384 digest as a hex string
408-
* @throws IOException On error reading from the stream
429+
* @throws IOException
430+
* On error reading from the stream
409431
* @since 1.4
410432
*/
411433
public static String sha384Hex(InputStream data) throws IOException {
@@ -451,7 +473,8 @@ public static byte[] sha512(byte[] data) {
451473
* @param data
452474
* Data to digest
453475
* @return SHA-512 digest
454-
* @throws IOException On error reading from the stream
476+
* @throws IOException
477+
* On error reading from the stream
455478
* @since 1.4
456479
*/
457480
public static byte[] sha512(InputStream data) throws IOException {
@@ -470,7 +493,7 @@ public static byte[] sha512(InputStream data) throws IOException {
470493
* @since 1.4
471494
*/
472495
public static byte[] sha512(String data) {
473-
return sha512(data.getBytes());
496+
return sha512(getBytesUtf8(data));
474497
}
475498

476499
/**
@@ -497,7 +520,8 @@ public static String sha512Hex(byte[] data) {
497520
* @param data
498521
* Data to digest
499522
* @return SHA-512 digest as a hex string
500-
* @throws IOException On error reading from the stream
523+
* @throws IOException
524+
* On error reading from the stream
501525
* @since 1.4
502526
*/
503527
public static String sha512Hex(InputStream data) throws IOException {
@@ -536,7 +560,8 @@ public static String shaHex(byte[] data) {
536560
* @param data
537561
* Data to digest
538562
* @return SHA-1 digest as a hex string
539-
* @throws IOException On error reading from the stream
563+
* @throws IOException
564+
* On error reading from the stream
540565
*/
541566
public static String shaHex(InputStream data) throws IOException {
542567
return new String(Hex.encodeHex(sha(data)));

src/test/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.IOException;
2222
import java.util.Random;
2323

24+
import org.apache.commons.codec.binary.StringUtils;
25+
2426
import junit.framework.TestCase;
2527

2628
/**
@@ -33,13 +35,21 @@ public class DigestUtilsTest extends TestCase {
3335

3436
private byte[] testData = new byte[1024*1024];
3537

38+
private byte[] getBytesUtf8(String hashMe) {
39+
return StringUtils.getBytesUtf8(hashMe);
40+
}
41+
3642
/* (non-Javadoc)
3743
* @see junit.framework.TestCase#setUp()
3844
*/
3945
protected void setUp() throws Exception {
4046
new Random().nextBytes(testData);
4147
}
4248

49+
public void testConstructable() {
50+
new DigestUtils();
51+
}
52+
4353
public void testInternalNoSuchAlgorithmException() {
4454
try {
4555
DigestUtils.getDigest("Bogus Bogus");
@@ -72,17 +82,17 @@ public void testMd5Hex() throws IOException {
7282
assertEquals(DigestUtils.md5Hex(testData),
7383
DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
7484
}
75-
85+
7686
/**
7787
* An MD5 hash converted to hex should always be 32 characters.
7888
*/
7989
public void testMD5HexLength() {
8090
String hashMe = "this is some string that is longer than 32 characters";
81-
String hash = DigestUtils.md5Hex(hashMe.getBytes());
91+
String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
8292
assertEquals(32, hash.length());
8393

8494
hashMe = "length < 32";
85-
hash = DigestUtils.md5Hex(hashMe.getBytes());
95+
hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
8696
assertEquals(32, hash.length());
8797
}
8898

@@ -91,11 +101,11 @@ public void testMD5HexLength() {
91101
*/
92102
public void testMD5Length() {
93103
String hashMe = "this is some string that is longer than 16 characters";
94-
byte[] hash = DigestUtils.md5(hashMe.getBytes());
104+
byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe));
95105
assertEquals(16, hash.length);
96106

97107
hashMe = "length < 16";
98-
hash = DigestUtils.md5(hashMe.getBytes());
108+
hash = DigestUtils.md5(getBytesUtf8(hashMe));
99109
assertEquals(16, hash.length);
100110
}
101111

@@ -104,13 +114,13 @@ public void testSha256() throws IOException {
104114
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
105115
DigestUtils.sha256Hex("abc"));
106116
assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
107-
DigestUtils.sha256Hex("abc".getBytes()));
117+
DigestUtils.sha256Hex(getBytesUtf8("abc")));
108118
assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
109119
DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
110120

111121
assertEquals(DigestUtils.sha256Hex(testData),
112122
DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
113-
}
123+
}
114124

115125
public void testSha384() throws IOException {
116126
// Examples from FIPS 180-2
@@ -119,14 +129,14 @@ public void testSha384() throws IOException {
119129
DigestUtils.sha384Hex("abc"));
120130
assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" +
121131
"8086072ba1e7cc2358baeca134c825a7",
122-
DigestUtils.sha384Hex("abc".getBytes()));
132+
DigestUtils.sha384Hex(getBytesUtf8("abc")));
123133
assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" +
124134
"fcc7c71a557e2db966c3e9fa91746039",
125135
DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
126136
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
127137
assertEquals(DigestUtils.sha384Hex(testData),
128138
DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
129-
}
139+
}
130140

131141
public void testSha512() throws IOException {
132142
// Examples from FIPS 180-2
@@ -135,30 +145,26 @@ public void testSha512() throws IOException {
135145
DigestUtils.sha512Hex("abc"));
136146
assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
137147
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
138-
DigestUtils.sha512Hex("abc".getBytes()));
148+
DigestUtils.sha512Hex(getBytesUtf8("abc")));
139149
assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
140150
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
141151
DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +
142152
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
143153
assertEquals(DigestUtils.sha512Hex(testData),
144154
DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
145-
}
155+
}
146156

147157
public void testShaHex() throws IOException {
148158
// Examples from FIPS 180-1
149159
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc"));
150160

151-
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes()));
161+
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc")));
152162

153163
assertEquals(
154164
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
155165
DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
156166
assertEquals(DigestUtils.shaHex(testData),
157167
DigestUtils.shaHex(new ByteArrayInputStream(testData)));
158168

159-
}
160-
161-
public void testConstructable() {
162-
new DigestUtils();
163169
}
164170
}

0 commit comments

Comments
 (0)