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)

1
$ echo -e "string to hash" | md5sum
2
2fcc83d76f9b2d8ef372271d807a3364 -
Copied!
Note that any white space either side of the string will also be hashed, creating a different hash
1
$ echo -e " string to hash " | md5sum
2
6b9202a3b05bd22803fe54767b30ee10 -
Copied!
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).
1
$ echo -e -n "" | md5sum
2
d41d8cd98f00b204e9800998ecf8427e -
Copied!

C\

Convert a string to a MD5 hash
1
static private string GetMd5Hash(string input) {
2
MD5 md5Hash = MD5.Create();
3
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
4
return ByteToString(data);
5
}
6
7
// Helper function to convert a byte array to a string.
8
static private string ByteToString(byte[] data) {
9
StringBuilder sBuilder = new StringBuilder();
10
for (int i = 0; i < data.Length; i++) {
11
sBuilder.Append(data[i].ToString("x2"));
12
}
13
return sBuilder.ToString();
14
}
Copied!

Java

1
import java.security.*;
2
import java.io.File;
3
import java.io.FileReader;
4
import java.io.IOException;
5
6
public class MD5Test
7
{
8
public static void main (String args[]) throws Exception {
9
String s = "string to hash";
10
MessageDigest md = MessageDigest.getInstance("MD5");
11
byte[] bytesOfMessage = s.getBytes("UTF-8");
12
byte[] thedigest = md.digest(bytesOfMessage);
13
System.out.println(
14
javax.xml.bind.DatatypeConverter.printHexBinary(thedigest).toLowerCase());
15
}
16
}
Copied!

Javascript

1
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
2
3
<script>
4
var hash = CryptoJS.MD5("string to hash\n");
5
alert("" + hash);
6
</script>
Copied!

Nodejs

This code assumes you have the crypto-js library installed (npm install crypto-js)
1
$ cat md5.js
2
var CryptoJS = require('crypto-js');
3
var hash = CryptoJS.MD5('string to hash\n');
4
console.log("" + hash);
5
6
$ node md5.js
7
2fcc83d76f9b2d8ef372271d807a3364
Copied!

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)
1
#import <CommonCrypto/CommonDigest.h>
2
...
3
+(NSString*) md5:(NSString*)input
4
{
5
const char *cInput = [input UTF8String];
6
unsigned char digest[CC_MD5_DIGEST_LENGTH];
7
CC_MD5(cInput, strlen(cInput), digest);
8
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
9
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
10
[output appendFormat:@"%02x", digest[i]];
11
}
12
return output;
13
}
14
...
Copied!

PHP

1
echo md5('string to hash\n')
Copied!