Hi all, I am trying to convert an encryption code written in C# to angular but I am not able to generate the hash, please take a look and help me on this. I am stuck in this from last week.
generateHash(value: string, salt: string) {
let result: any;
let saltBytes: any;
var encoder = new TextEncoder();
try {
if (salt == null) {
console.log('salt is empty');
}
else {
saltBytes = encoder.encode(salt);
}
let valueBytes = encoder.encode(value);
// Add the salt to the hash.
//rawSalted = valueBytes+','+ saltBytes;
var rawSalted = new Uint8Array(valueBytes.length + saltBytes.length);
rawSalted.set(valueBytes);
rawSalted.set(saltBytes, valueBytes.length);
//////////////////////////Working fine till here;
//Create the salted hash.
let hash = new CryptoJS.SHA384(rawSalted);
//alert(hash);
let saltedValue = hash.ComputerHash(rawSalted);
alert(saltedValue);
//hash.Clear();
// Add the salt value to the salted hash.
let saltHashResult = new Uint8Array[saltedValue.Length + saltBytes.Length];
saltedValue.CopyTo(saltHashResult, 0);
saltBytes.CopyTo(saltHashResult, saltedValue.Length);
result = this.string2Bin(saltHashResult);
}
catch (e) {
console.log(e);
return result;
}
return result;
}
Here is the C# code:
public static string GenerateHash(string value, string salt) {
string result = string.Empty;
byte[] saltBytes = null;
try {
if (string.IsNullOrEmpty(salt)) {
throw new Exception("Parameter: salt cannot be blank.");
}
else {
saltBytes = UTF8Encoding.UTF8.GetBytes(salt);
}
byte[] valueBytes = UTF8Encoding.UTF8.GetBytes(value);
// Add the salt to the hash.
byte[] rawSalted = new byte[valueBytes.Length + saltBytes.Length];
valueBytes.CopyTo(rawSalted, 0);
saltBytes.CopyTo(rawSalted, valueBytes.Length);
//Create the salted hash.
SHA384Managed hash = new SHA384Managed();
byte[] saltedValue = hash.ComputeHash(rawSalted);
hash.Clear();
// Add the salt value to the salted hash.
byte[] saltHashResult = new byte[saltedValue.Length + saltBytes.Length];
saltedValue.CopyTo(saltHashResult, 0);
saltBytes.CopyTo(saltHashResult, saltedValue.Length);
result = Convert.ToBase64String(saltHashResult);
}
catch {
return result;
}
return result;
}