Reputation: 2018
I have some code written in javascript that is as follows which is working properly:
var generateSignature = function(apiKey, apiSecret, meetingNumber, role) {
// Prevent time sync issue between client signature generation and zoom
var timestamp = new Date().getTime() - 30000;
var msg = btoa(apiKey + meetingNumber + timestamp + role);
var hash = window.CryptoJS.HmacSHA256(msg, apiSecret);
var hashedBaseItems = CryptoJS.enc.Base64.stringify(hash);
var encodableString = apiKey + "." + meetingNumber + "." + timestamp + "." + role + "." + hashedBaseItems;
signature = btoa(encodableString);
return signature;
}
I'm trying to convert them to C# equivalents, but with mixed results. Here is what I have so far:
public string GenerateSignature(int role, long meetingNumber)
{
// Prevent time sync issue between client signature generation and zoom
long timeStamp = this.getLinuxEpochTimestamp() - 30000;
string baseItems = _plusConfig.Value.ZoomApiKey + meetingNumber + timeStamp + role;
string msg = this.jsbtoaEquivalentEncoding(baseItems);
string hashedBaseItems = generateHash(msg, _plusConfig.Value.ZoomApiSecret);
string dottedItems = _plusConfig.Value.ZoomApiKey + "." + meetingNumber + "." + timeStamp + "." + role + "." + hashedBaseItems;
return this.jsbtoaEquivalentEncoding(dottedItems);
}
private long getLinuxEpochTimestamp()
{
return (long) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
private string jsbtoaEquivalentEncoding(string toEncode)
{
byte[] bytes = Encoding.GetEncoding(28591).GetBytes(toEncode);
string toReturn = System.Convert.ToBase64String(bytes);
return toReturn;
}
private static string generateHash(string str, string cypherkey)
{
// based on CryptoJS.enc.Base64.parse
byte[] keyBytes = Convert.FromBase64String(cypherkey);
using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes))
{
byte[] hashmessage = hmacsha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
return Convert.ToBase64String(hashmessage);
}
}
What am I doing wrong where the generateHash
function is returning something completely different than the CryptoJS.HmacSHA256 function?
Upvotes: 1
Views: 2181
Reputation: 242
private static string generateHash(string str, string cypherkey)
{
var encoding = new System.Text.ASCIIEncoding();
var messageBytes = encoding.GetBytes(str);
var keyBytes = encoding.GetBytes(cypherkey);
using var hmacsha256 = new HMACSHA256(keyBytes);
var hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
Should solve the issue.
Upvotes: 2