Skip to content

Commit 112a19d

Browse files
authored
Add RequestBody.sha256() (#9109)
* Add RequestBody.sha256() * * SHA256 -> SHA-256 * Return ByteString, not String * simplify test * Move extension function to a regular class method * Add @throws(IOException::class)
1 parent d41a755 commit 112a19d

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

okhttp/api/android/okhttp.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ public abstract class okhttp3/RequestBody {
10781078
public static final fun create ([BLokhttp3/MediaType;II)Lokhttp3/RequestBody;
10791079
public fun isDuplex ()Z
10801080
public fun isOneShot ()Z
1081+
public final fun sha256 ()Lokio/ByteString;
10811082
public abstract fun writeTo (Lokio/BufferedSink;)V
10821083
}
10831084

okhttp/api/jvm/okhttp.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ public abstract class okhttp3/RequestBody {
10771077
public static final fun create ([BLokhttp3/MediaType;II)Lokhttp3/RequestBody;
10781078
public fun isDuplex ()Z
10791079
public fun isOneShot ()Z
1080+
public final fun sha256 ()Lokio/ByteString;
10801081
public abstract fun writeTo (Lokio/BufferedSink;)V
10811082
}
10821083

okhttp/src/commonJvmAndroid/kotlin/okhttp3/Request.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,7 @@ class Request internal constructor(
330330
* A typical use case is to hash the request body:
331331
*
332332
* ```kotlin
333-
* val hashingSink = HashingSink.md5(blackholeSink())
334-
* hashingSink.buffer().use {
335-
* body.writeTo(it)
336-
* }
337-
* val hash = hashingSink.hash.hex()
333+
* val hash = body.sha256().hex()
338334
* val query = Request
339335
* .Builder()
340336
* .query(body)

okhttp/src/commonJvmAndroid/kotlin/okhttp3/RequestBody.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import okhttp3.internal.chooseCharset
2424
import okio.BufferedSink
2525
import okio.ByteString
2626
import okio.FileSystem
27+
import okio.HashingSink
2728
import okio.Path
29+
import okio.blackholeSink
30+
import okio.buffer
2831
import okio.source
2932

3033
abstract class RequestBody {
@@ -95,6 +98,18 @@ abstract class RequestBody {
9598
*/
9699
open fun isOneShot(): Boolean = false
97100

101+
/**
102+
* Returns the SHA-256 hash of this [RequestBody]
103+
*/
104+
@Throws(IOException::class)
105+
fun sha256(): ByteString {
106+
val hashingSink = HashingSink.sha256(blackholeSink())
107+
hashingSink.buffer().use {
108+
this.writeTo(it)
109+
}
110+
return hashingSink.hash
111+
}
112+
98113
companion object {
99114
/** Empty request body with no content-type. */
100115
@JvmField

okhttp/src/jvmTest/kotlin/okhttp3/RequestBodyTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ class RequestBodyTest {
119119
}
120120
}
121121

122+
@Test
123+
fun testSha256() {
124+
val hash = "Hello".toRequestBody().sha256().hex()
125+
assertThat(hash).isEqualTo("185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969")
126+
}
127+
122128
private inline fun <T> assertOnFileDescriptor(
123129
content: String? = null,
124130
fn: (FileDescriptor) -> T,

0 commit comments

Comments
 (0)