To make transportation more secure and accurate, we can use checksum.
On the server, we regenerate the checksum to see whether it's the same as the one that sent in. If the same, meaning that the message has not been modified, during transportation.
There are two ways to generate checksum, one is to use "SHA1", the other is to use "MD5"
Use "SHA1" to generate digest:
01 public String generateDigest(byte[] payload){
02
03 String digestGen = null;
04 try {
05 MessageDigest mDigest = MessageDigest.getInstance("SHA1");
06 byte[] result = mDigest.digest(payload);
07 StringBuffer sb = new StringBuffer();
08 for (int i = 0; i < result.length; i++) {
09 sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
10 }
11 digestGen = sb.toString();
12 } catch (Exception e) {
13 LOGGER.error("Cannot generate digest!",e);
14 }
15 return digestGen;
16 }
We can generate the checksum before we encode the bytes for transportation.
Because Base64 bytes can assume more time to generate checksum. Efficiency will not be that good.
No comments:
Post a Comment