Using the 'standard' HMACSHA256 technique in dotnetcore C# I can produce a hashed string as follows:
private static void Test()
{
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes("testingkey"));
var theHash = hmac.ComputeHash(Encoding.UTF8.GetBytes("testingstringtohash"));
string signature = Convert.ToBase64String(theHash);
Console.WriteLine(signature);
}
//Produces yg/9NCAm5IIwGKJK80PyUeBWkzEUwZswvC3OVnTnT80=
To do the same in swift (solution from this answer seems to be the 'standard' that people are using)
func HashTest() {
let hash = "testingstringtohash".hmac(algorithm: .SHA256, key: "testingkey")
//hash ca0ffd342026e4823018a24af343f251e056933114c19b30bc2dce5674e74fcd
let hexData = hash.data(using: String.Encoding.utf8)
let signature = hexData?.base64EncodedString()
print(signature!)
}
//Produces Y2EwZmZkMzQyMDI2ZTQ4MjMwMThhMjRhZjM0M2YyNTFlMDU2OTMzMTE0YzE5YjMwYmMyZGNlNTY3NGU3NGZjZA==
Am I being stupid here... or should these two values be the same, as it is the same encryption algorithm, and the same key for the same value. As far as I can tell the C# example produces the 'correct' result, as a webservice that is consuming a value produced with that example works fine, but the value that the swift version produces is failing.