/// <summary> /// calcolo hash di un file /// </summary> /// <param name="pathFile">path and file</param> /// <param name="lastError">ultimo errore nella funzionalità</param> /// <param name="algorithm">algoritmo da utilizzare</param> /// <param name="encode">encode</param> /// <returns>hash del file</returns> public static string HashFile(string pathFile, ref string lastError, string algorithm = "sha256", string encode = "base64") { string hash = null; try { if (Utilities.glob.UnlockStatus == 0) { lastError = "Licenza bloccata"; return(hash); } Chilkat.Crypt2 crypt = new Chilkat.Crypt2(); crypt.HashAlgorithm = algorithm; crypt.EncodingMode = encode; hash = crypt.HashFileENC(pathFile); } catch { throw; } return(hash); }
/// <summary> /// applica la marca temporale al file /// </summary> /// <param name="pathFileSign">file firmato</param> /// <param name="tsaUrl">url TSA</param> /// <param name="pathFileTimeStamped">file tsr da TSA</param> /// <param name="lastError">ultimo errore nella funzionalità</param> /// <param name="userName">user TSA (opzionale)</param> /// <param name="password">password TSA (opzionale)</param> /// <returns>true se la funzionalità ha avuto successo</returns> /// <example> /// if (Utilities.MarcaTemporale(@"c:\temp\IT01234567890_FPA01.xml.p7m", "https://freetsa.org/tsr", out pathFileTimeStamped, ref lastError, "myUser", "myPassword")) /// { /// pathFileTimeStamped -> c:\temp\IT01234567890_FPA01.xml.p7m.tsr /// } /// </example> public static bool MarcaTemporale(string pathFileSign, string tsaUrl, out string pathFileTimeStamped, ref string lastError, string userName = null, string password = null) { bool success = false; pathFileTimeStamped = null; try { string fileName = Path.GetFileName(pathFileSign); if (Utilities.glob.UnlockStatus == 0) { lastError = "Licenza bloccata"; return(success); } Chilkat.Crypt2 crypt = new Chilkat.Crypt2(); crypt.HashAlgorithm = "sha256"; crypt.EncodingMode = "base64"; string base64Hash = crypt.HashFileENC(pathFileSign); Chilkat.Http http = new Chilkat.Http(); Chilkat.BinData requestToken = new Chilkat.BinData(); string optionalPolicyOid = string.Empty; bool addNonce = false; bool requestTsaCert = false; if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password)) { http.Login = userName; http.Password = password; http.BasicAuth = true; } // Create a time-stamp request token success = http.CreateTimestampRequest("sha256", base64Hash, optionalPolicyOid, addNonce, requestTsaCert, requestToken); if (!success) { lastError = http.LastErrorText; return(success); } Chilkat.HttpResponse resp = http.PBinaryBd("POST", tsaUrl, requestToken, "application/timestamp-query", false, false); if (!http.LastMethodSuccess) { lastError = http.LastErrorText; return(success); } Chilkat.BinData timestampReply = new Chilkat.BinData(); resp.GetBodyBd(timestampReply); if (!timestampReply.LastMethodSuccess) { return(success); } string s = Path.ChangeExtension(fileName, $".{Enum.GetName(typeof(EstensioniFile), EstensioniFile.tsr)}"); success = timestampReply.WriteFile(s); if (success) { pathFileTimeStamped = s; } } catch { throw; } return(success); }