Skip to content

Instantly share code, notes, and snippets.

@balrampandey19
Created December 29, 2016 07:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save balrampandey19/96bdc04174e58ba3dd03c0aa6f7345af to your computer and use it in GitHub Desktop.
Save balrampandey19/96bdc04174e58ba3dd03c0aa6f7345af to your computer and use it in GitHub Desktop.
Encrypt retrofit request body with SHA256
/**
* 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