File tree Expand file tree Collapse file tree 5 files changed +24
-5
lines changed 
commonJvmAndroid/kotlin/okhttp3 Expand file tree Collapse file tree 5 files changed +24
-5
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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) 
Original file line number Diff line number Diff line change @@ -24,7 +24,10 @@ import okhttp3.internal.chooseCharset
2424import  okio.BufferedSink 
2525import  okio.ByteString 
2626import  okio.FileSystem 
27+ import  okio.HashingSink 
2728import  okio.Path 
29+ import  okio.blackholeSink 
30+ import  okio.buffer 
2831import  okio.source 
2932
3033abstract  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
Original file line number Diff line number Diff line change @@ -119,6 +119,12 @@ class RequestBodyTest {
119119    }
120120  }
121121
122+   @Test
123+   fun  testSha256 () {
124+     val  hash =  " Hello" 
125+     assertThat(hash).isEqualTo(" 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" 
126+   }
127+ 
122128  private  inline  fun  <T > assertOnFileDescriptor (
123129    content :  String?  = null,
124130    fn :  (FileDescriptor ) ->  T ,
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments