1 /* 2 * Copyright 2014 Square Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package okio 17 18 import okio.ByteString.Companion.decodeHex 19 import okio.ByteString.Companion.encodeUtf8 20 import kotlin.test.Test 21 import kotlin.test.assertEquals 22 23 class HashingTest { byteStringMd5null24 @Test fun byteStringMd5() { 25 assertEquals(MD5_abc, "abc".encodeUtf8().md5()) 26 } 27 byteStringSha1null28 @Test fun byteStringSha1() { 29 assertEquals(SHA1_abc, "abc".encodeUtf8().sha1()) 30 } 31 byteStringSha256null32 @Test fun byteStringSha256() { 33 assertEquals(SHA256_abc, "abc".encodeUtf8().sha256()) 34 } 35 byteStringSha512null36 @Test fun byteStringSha512() { 37 assertEquals(SHA512_abc, "abc".encodeUtf8().sha512()) 38 } 39 byteStringHmacSha1null40 @Test fun byteStringHmacSha1() { 41 assertEquals(HMAC_SHA1_abc, "abc".encodeUtf8().hmacSha1(HMAC_KEY)) 42 } 43 byteStringHmacSha256null44 @Test fun byteStringHmacSha256() { 45 assertEquals(HMAC_SHA256_abc, "abc".encodeUtf8().hmacSha256(HMAC_KEY)) 46 } 47 byteStringHmacSha512null48 @Test fun byteStringHmacSha512() { 49 assertEquals(HMAC_SHA512_abc, "abc".encodeUtf8().hmacSha512(HMAC_KEY)) 50 } 51 bufferMd5null52 @Test fun bufferMd5() { 53 assertEquals(MD5_abc, Buffer().writeUtf8("abc").md5()) 54 } 55 bufferSha1null56 @Test fun bufferSha1() { 57 assertEquals(SHA1_abc, Buffer().writeUtf8("abc").sha1()) 58 } 59 bufferSha256null60 @Test fun bufferSha256() { 61 assertEquals(SHA256_abc, Buffer().writeUtf8("abc").sha256()) 62 } 63 bufferSha512null64 @Test fun bufferSha512() { 65 assertEquals(SHA512_abc, Buffer().writeUtf8("abc").sha512()) 66 } 67 hashEmptySha256Buffernull68 @Test fun hashEmptySha256Buffer() { 69 assertEquals(SHA256_empty, Buffer().sha256()) 70 } 71 hashEmptySha512Buffernull72 @Test fun hashEmptySha512Buffer() { 73 assertEquals(SHA512_empty, Buffer().sha512()) 74 } 75 bufferHmacSha1null76 @Test fun bufferHmacSha1() { 77 assertEquals(HMAC_SHA1_abc, Buffer().writeUtf8("abc").hmacSha1(HMAC_KEY)) 78 } 79 bufferHmacSha256null80 @Test fun bufferHmacSha256() { 81 assertEquals(HMAC_SHA256_abc, Buffer().writeUtf8("abc").hmacSha256(HMAC_KEY)) 82 } 83 bufferHmacSha512null84 @Test fun bufferHmacSha512() { 85 assertEquals(HMAC_SHA512_abc, Buffer().writeUtf8("abc").hmacSha512(HMAC_KEY)) 86 } 87 hmacSha256EmptyBuffernull88 @Test fun hmacSha256EmptyBuffer() { 89 assertEquals(HMAC_SHA256_empty, Buffer().sha256()) 90 } 91 hmacSha512EmptyBuffernull92 @Test fun hmacSha512EmptyBuffer() { 93 assertEquals(HMAC_SHA512_empty, Buffer().sha512()) 94 } 95 bufferHashIsNotDestructivenull96 @Test fun bufferHashIsNotDestructive() { 97 val buffer = Buffer() 98 99 buffer.writeUtf8("abc") 100 assertEquals(SHA256_abc, buffer.sha256()) 101 assertEquals("abc", buffer.readUtf8()) 102 103 buffer.writeUtf8("def") 104 assertEquals(SHA256_def, buffer.sha256()) 105 assertEquals("def", buffer.readUtf8()) 106 107 buffer.write(r32k) 108 assertEquals(SHA256_r32k, buffer.sha256()) 109 assertEquals(r32k, buffer.readByteString()) 110 } 111 112 companion object { 113 val HMAC_KEY = 114 "0102030405060708".decodeHex() 115 val MD5_abc = 116 "900150983cd24fb0d6963f7d28e17f72".decodeHex() 117 val SHA1_abc = 118 "a9993e364706816aba3e25717850c26c9cd0d89d".decodeHex() 119 val HMAC_SHA1_abc = 120 "987af8649982ff7d9fbb1b8aa35099146997af51".decodeHex() 121 val SHA256_abc = 122 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".decodeHex() 123 val SHA256_empty = 124 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".decodeHex() 125 val SHA256_def = 126 "cb8379ac2098aa165029e3938a51da0bcecfc008fd6795f401178647f96c5b34".decodeHex() 127 val SHA256_r32k = 128 "dadec7297f49bdf219895bd9942454047d394e1f20f247fbdc591080b4e8731e".decodeHex() 129 val SHA512_abc = 130 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f".decodeHex() 131 val SHA512_empty = 132 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e".decodeHex() 133 val HMAC_SHA256_empty = 134 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".decodeHex() 135 val HMAC_SHA256_abc = 136 "446d1715583cf1c30dfffbec0df4ff1f9d39d493211ab4c97ed6f3f0eb579b47".decodeHex() 137 val HMAC_SHA512_empty = 138 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e".decodeHex() 139 val HMAC_SHA512_abc = 140 "24391790e7131050b05b606f2079a8983313894a1642a5ed97d094e7cabd00cfaa857d92c1f320ca3b6aaabb84c7155d6f1b10940dc133ded1b40baee8900be6".decodeHex() 141 val r32k = randomBytes(32768) 142 } 143 } 144