Created
December 29, 2016 07:24
-
-
Save balrampandey19/96bdc04174e58ba3dd03c0aa6f7345af to your computer and use it in GitHub Desktop.
Encrypt retrofit request body with SHA256
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Balram Pandey on 12/29/16. | |
*/ | |
public class EncryptionInterceptor implements Interceptor { | |
private static final String TAG = EncryptionInterceptor.class.getSimpleName(); | |
private static final boolean DEBUG = true; | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
RequestBody oldBody = request.body(); | |
Buffer buffer = new Buffer(); | |
oldBody.writeTo(buffer); | |
String strOldBody = buffer.readUtf8(); | |
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); | |
String strNewBody = encrypt(strOldBody); | |
RequestBody body = RequestBody.create(mediaType, strNewBody); | |
request = request.newBuilder(). | |
.header("User-Agent", "Your-App-Name"); | |
return chain.proceed(request); | |
} | |
private static String encrypt(String body){ | |
String encryptBody=""; | |
try { | |
encryptBody = Crypto.getHmacSha256("KEY", body); | |
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) { | |
e.printStackTrace(); | |
} | |
return encryptBody; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment