I'm working on asp.net MVC Project to sign and send e-invoice as per ITIDA Egypt standards for Egypt E-Invoicing
I used Signer.Digital Browser Extension provided from Signer.Digital
as per Sign hash using CADES-BES signature using USB token
My problem is when I sign an invoice become invalid like this
enter image description here
Note: My steps to sign the data 1- I prepare the invoice json
2- I creating a canonical string from json an i got somthing like that https://sdk.preprod.invoicing.eta.gov.eg/files/one-doc-serialized.json.txt
3- hashing the serialized string by (what I think the propelm is )
byte[] result = Encoding.UTF8.GetBytes(canonicalString);
this code is not working for me then I tried
var result = GetHash(canonicalString, System.Text.Encoding.UTF8);
public static string GetHash(string value, System.Text.Encoding encoding)
{
string result = string.Empty;
using (SHA256 mySHA256 = SHA256.Create())
{
byte[] valueArr = null;
if (encoding == System.Text.Encoding.ASCII)
valueArr = System.Text.Encoding.ASCII.GetBytes(value); //convert string value to byte[]
else if (encoding == System.Text.Encoding.Default)
valueArr = System.Text.Encoding.Default.GetBytes(value); //convert string value to byte[]
else if (encoding == System.Text.Encoding.UTF7)
valueArr = System.Text.Encoding.UTF7.GetBytes(value); //convert string value to byte[]
else if (encoding == System.Text.Encoding.UTF8)
valueArr = System.Text.Encoding.UTF8.GetBytes(value); //convert string value to byte[]
else if (encoding == System.Text.Encoding.UTF32)
valueArr = System.Text.Encoding.UTF32.GetBytes(value); //convert string value to byte[]
else
throw new Exception("Encoding not supported");
using (MemoryStream ms = new MemoryStream(valueArr))
{
//compute hash
byte[] hashValue = mySHA256.ComputeHash(ms);
//for testing, output hash value as HEX
System.Diagnostics.Debug.WriteLine("hashValue.Length: " + hashValue.Length + " bytes");
for (int i = 0; i < hashValue.Length; i++)
{
System.Diagnostics.Debug.Write($"{hashValue[i]:X2}");
if ((i % 4) == 3)
System.Diagnostics.Debug.Write(" ");
}
//convert to Base64 string
result = Convert.ToBase64String(hashValue);
//convert to string
//result = System.Text.Encoding.UTF8.GetString(hashValue);
System.Diagnostics.Debug.WriteLine("result: " + result);
}
}
return result;
}
4- then I Pass the result to javascript
SignResult = await SignerDigital.signHashCAdESEg(result);
5- prepare full Document Json by adding signature result to Json and "{"documents":[" things then posting the invoice.