Skip to content

Commit c45a7d3

Browse files
committed
[CODEC-156] DigestUtils: add APIs named after standard alg name SHA-1.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1380022 13f79535-47bb-0310-9956-ffa450edef68
1 parent d915d75 commit c45a7d3

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,55 @@ public static byte[] sha1(InputStream data) throws IOException {
398398
return digest(getSha1Digest(), data);
399399
}
400400

401+
/**
402+
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
403+
*
404+
* @param data
405+
* Data to digest
406+
* @return SHA-1 digest
407+
*/
408+
public static byte[] sha1(String data) {
409+
return sha1(getBytesUtf8(data));
410+
}
411+
412+
/**
413+
* Calculates the SHA-1 digest and returns the value as a hex string.
414+
*
415+
* @param data
416+
* Data to digest
417+
* @return SHA-1 digest as a hex string
418+
* @since 1.7
419+
*/
420+
public static String sha1Hex(byte[] data) {
421+
return Hex.encodeHexString(sha1(data));
422+
}
423+
424+
/**
425+
* Calculates the SHA-1 digest and returns the value as a hex string.
426+
*
427+
* @param data
428+
* Data to digest
429+
* @return SHA-1 digest as a hex string
430+
* @throws IOException
431+
* On error reading from the stream
432+
* @since 1.7
433+
*/
434+
public static String sha1Hex(InputStream data) throws IOException {
435+
return Hex.encodeHexString(sha1(data));
436+
}
437+
438+
/**
439+
* Calculates the SHA-1 digest and returns the value as a hex string.
440+
*
441+
* @param data
442+
* Data to digest
443+
* @return SHA-1 digest as a hex string
444+
* @since 1.7
445+
*/
446+
public static String sha1Hex(String data) {
447+
return Hex.encodeHexString(sha1(data));
448+
}
449+
401450
/**
402451
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
403452
* <p>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ public void testShaHex() throws IOException {
233233
DigestUtils.shaHex(new ByteArrayInputStream(testData)));
234234
}
235235

236+
@Test
237+
public void testSha1Hex() throws IOException {
238+
// Examples from FIPS 180-1
239+
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc"));
240+
241+
assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc")));
242+
243+
assertEquals(
244+
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
245+
DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
246+
assertEquals(DigestUtils.shaHex(testData),
247+
DigestUtils.shaHex(new ByteArrayInputStream(testData)));
248+
}
249+
236250
@Test
237251
public void testSha1UpdateWithByteArray(){
238252
final String d1 = "C'est un homme qui rentre dans un café, et plouf";

0 commit comments

Comments
 (0)