Skip to content

Commit

Permalink
Add sha-512 hash and hmac support
Browse files Browse the repository at this point in the history
  • Loading branch information
rockaport committed Dec 23, 2016
1 parent 7c2f08b commit ef6d1e0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
10 changes: 10 additions & 0 deletions okio/src/main/java/okio/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,11 @@ public ByteString sha256() {
return digest("SHA-256");
}

/** Returns the 512-bit SHA-512 hash of this buffer. */
public ByteString sha512() {
return digest("SHA-512");
}

private ByteString digest(String algorithm) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
Expand All @@ -1563,6 +1568,11 @@ public ByteString hmacSha256(ByteString key) {
return hmac("HmacSHA256", key);
}

/** Returns the 512-bit SHA-512 HMAC of this buffer. */
public ByteString hmacSha512(ByteString key) {
return hmac("HmacSHA512", key);
}

private ByteString hmac(String algorithm, ByteString key) {
try {
Mac mac = Mac.getInstance(algorithm);
Expand Down
10 changes: 10 additions & 0 deletions okio/src/main/java/okio/ByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public ByteString sha256() {
return digest("SHA-256");
}

/** Returns the 512-bit SHA-512 hash of this byte string. */
public ByteString sha512() {
return digest("SHA-512");
}

private ByteString digest(String algorithm) {
try {
return ByteString.of(MessageDigest.getInstance(algorithm).digest(data));
Expand All @@ -163,6 +168,11 @@ public ByteString hmacSha256(ByteString key) {
return hmac("HmacSHA256", key);
}

/** Returns the 512-bit SHA-512 HMAC of this byte string. */
public ByteString hmacSha512(ByteString key) {
return hmac("HmacSHA512", key);
}

private ByteString hmac(String algorithm, ByteString key) {
try {
Mac mac = Mac.getInstance(algorithm);
Expand Down
10 changes: 10 additions & 0 deletions okio/src/main/java/okio/HashingSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public static HashingSink sha256(Sink sink) {
return new HashingSink(sink, "SHA-256");
}

/** Returns a sink that uses the SHA-512 hash algorithm to produce 512-bit hashes. */
public static HashingSink sha512(Sink sink) {
return new HashingSink(sink, "SHA-512");
}

/** Returns a sink that uses the obsolete SHA-1 HMAC algorithm to produce 160-bit hashes. */
public static HashingSink hmacSha1(Sink sink, ByteString key) {
return new HashingSink(sink, key, "HmacSHA1");
Expand All @@ -69,6 +74,11 @@ public static HashingSink hmacSha256(Sink sink, ByteString key) {
return new HashingSink(sink, key, "HmacSHA256");
}

/** Returns a sink that uses the SHA-512 HMAC algorithm to produce 512-bit hashes. */
public static HashingSink hmacSha512(Sink sink, ByteString key) {
return new HashingSink(sink, key, "HmacSHA512");
}

private HashingSink(Sink sink, String algorithm) {
super(sink);
try {
Expand Down
16 changes: 16 additions & 0 deletions okio/src/test/java/okio/HashingSinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import static okio.HashingTest.HMAC_KEY;
import static okio.HashingTest.HMAC_SHA1_abc;
import static okio.HashingTest.HMAC_SHA256_abc;
import static okio.HashingTest.HMAC_SHA512_abc;
import static okio.HashingTest.MD5_abc;
import static okio.HashingTest.SHA1_abc;
import static okio.HashingTest.SHA256_abc;
import static okio.HashingTest.SHA256_def;
import static okio.HashingTest.SHA256_r32k;
import static okio.HashingTest.r32k;
import static okio.HashingTest.SHA512_abc;
import static org.junit.Assert.assertEquals;

public final class HashingSinkTest {
Expand Down Expand Up @@ -53,6 +55,13 @@ public final class HashingSinkTest {
assertEquals(SHA256_abc, hashingSink.hash());
}

@Test public void sha512() throws Exception {
HashingSink hashingSink = HashingSink.sha512(sink);
source.writeUtf8("abc");
hashingSink.write(source, 3L);
assertEquals(SHA512_abc, hashingSink.hash());
}

@Test public void hmacSha1() throws Exception {
HashingSink hashingSink = HashingSink.hmacSha1(sink, HMAC_KEY);
source.writeUtf8("abc");
Expand All @@ -67,6 +76,13 @@ public final class HashingSinkTest {
assertEquals(HMAC_SHA256_abc, hashingSink.hash());
}

@Test public void hmacSha512() throws Exception {
HashingSink hashingSink = HashingSink.hmacSha512(sink, HMAC_KEY);
source.writeUtf8("abc");
hashingSink.write(source, 3L);
assertEquals(HMAC_SHA512_abc, hashingSink.hash());
}

@Test public void multipleWrites() throws Exception {
HashingSink hashingSink = HashingSink.sha256(sink);
source.writeUtf8("a");
Expand Down
36 changes: 34 additions & 2 deletions okio/src/test/java/okio/HashingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ public final class HashingTest {
"cb8379ac2098aa165029e3938a51da0bcecfc008fd6795f401178647f96c5b34");
public static final ByteString SHA256_r32k = ByteString.decodeHex(
"3a640aa4d129671cb36a4bfbed652d732bce6b7ea83ccdd080c485b8c9ef479d");
public static final ByteString SHA512_abc = ByteString.decodeHex(
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
public static final ByteString SHA512_empty = ByteString.decodeHex(
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
public static final ByteString HMAC_SHA256_empty = ByteString.decodeHex(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
public static final ByteString HMAC_SHA256_abc = ByteString.decodeHex(
"446d1715583cf1c30dfffbec0df4ff1f9d39d493211ab4c97ed6f3f0eb579b47");
public static final ByteString HMAC_SHA512_empty = ByteString.decodeHex(
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
public static final ByteString HMAC_SHA512_abc = ByteString.decodeHex(
"24391790e7131050b05b606f2079a8983313894a1642a5ed97d094e7cabd00cfaa857d92c1f320ca3b6aaabb84c7155d6f1b10940dc133ded1b40baee8900be6");
public static final ByteString r32k = TestUtil.randomBytes(32768);

@Test public void byteStringMd5() {
Expand All @@ -53,6 +61,10 @@ public final class HashingTest {
assertEquals(SHA256_abc, ByteString.encodeUtf8("abc").sha256());
}

@Test public void byteStringSha512() {
assertEquals(SHA512_abc, ByteString.encodeUtf8("abc").sha512());
}

@Test public void byteStringHmacSha1() {
assertEquals(HMAC_SHA1_abc, ByteString.encodeUtf8("abc").hmacSha1(HMAC_KEY));
}
Expand All @@ -61,6 +73,10 @@ public final class HashingTest {
assertEquals(HMAC_SHA256_abc, ByteString.encodeUtf8("abc").hmacSha256(HMAC_KEY));
}

@Test public void byteStringHmacSha512() {
assertEquals(HMAC_SHA512_abc, ByteString.encodeUtf8("abc").hmacSha512(HMAC_KEY));
}

@Test public void bufferMd5() {
assertEquals(MD5_abc, new Buffer().writeUtf8("abc").md5());
}
Expand All @@ -73,10 +89,18 @@ public final class HashingTest {
assertEquals(SHA256_abc, new Buffer().writeUtf8("abc").sha256());
}

@Test public void hashEmptyBuffer() {
@Test public void bufferSha512() {
assertEquals(SHA512_abc, new Buffer().writeUtf8("abc").sha512());
}

@Test public void hashEmptySha256Buffer() {
assertEquals(SHA256_empty, new Buffer().sha256());
}

@Test public void hashEmptySha512Buffer() {
assertEquals(SHA512_empty, new Buffer().sha512());
}

@Test public void bufferHmacSha1() {
assertEquals(HMAC_SHA1_abc, new Buffer().writeUtf8("abc").hmacSha1(HMAC_KEY));
}
Expand All @@ -85,10 +109,18 @@ public final class HashingTest {
assertEquals(HMAC_SHA256_abc, new Buffer().writeUtf8("abc").hmacSha256(HMAC_KEY));
}

@Test public void hmacEmptyBuffer() {
@Test public void bufferHmacSha512() {
assertEquals(HMAC_SHA512_abc, new Buffer().writeUtf8("abc").hmacSha512(HMAC_KEY));
}

@Test public void hmacSha256EmptyBuffer() {
assertEquals(HMAC_SHA256_empty, new Buffer().sha256());
}

@Test public void hmacSha512EmptyBuffer() {
assertEquals(HMAC_SHA512_empty, new Buffer().sha512());
}

@Test public void bufferHashIsNotDestructive() {
Buffer buffer = new Buffer();

Expand Down

0 comments on commit ef6d1e0

Please sign in to comment.