Calculating md5
The body of the request must have an MD5 hash calculated to ensure the body has not been altered during transport. It is important this is calculated the same as the iVvy API server to ensure the signature is calculated correctly and the request is authorized.
Bash (Linux Command Line)
$ echo -e "string to hash" | md5sum
2fcc83d76f9b2d8ef372271d807a3364 -Note that any white space either side of the string will also be hashed, creating a different hash
$ echo -e " string to hash " | md5sum
6b9202a3b05bd22803fe54767b30ee10 -It is important you strip off any whitespace before hashing the string.
Note also it is possible to hash an empty string (in the case there is no body in the request).
$ echo -e -n "" | md5sum
d41d8cd98f00b204e9800998ecf8427e -C\
Convert a string to a MD5 hash
static private string GetMd5Hash(string input) {
MD5 md5Hash = MD5.Create();
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
return ByteToString(data);
}
// Helper function to convert a byte array to a string.
static private string ByteToString(byte[] data) {
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++) {
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}Java
Javascript
Nodejs
This code assumes you have the crypto-js library installed (npm install crypto-js)
Objective-C
MD5 funcationlity can be be included in IOS code by first adding the CommonCrypto library to your target, and including the following function in your appDelegate (or appropriate class)
PHP
Last updated