public static X509Certificate2 GetCertificateFromFile(string certPath) { return new X509Certificate2(certPath); }
c# возвращаем сертификат из файла / C# get certificate from .crt .pfx file
c# Возвращаем сертификат из хранилища Windows сертификатов / c# get certificate from windows store
public static X509Certificate2 GetCertificateFromStore(string certName) { X509Store store = new X509Store(StoreLocation.CurrentUser); try { store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = store.Certificates; X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); X509Certificate2 o = currentCerts[0]; X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindBySubjectName, certName, false); if (signingCert.Count == 0) return null; return signingCert[0]; } finally { store.Close(); } }
c# подписываем файл сертификатом,полученную сигнатуру записываем в файл
public static bool CertificateSign(string signFilePath, string dumpFilePath, RSACryptoServiceProvider rsaKey) { byte[] data = null; using (System.IO.FileStream _FileStream = new System.IO.FileStream(dumpFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) using (System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream)) { long _TotalBytes = new System.IO.FileInfo(dumpFilePath).Length; data = _BinaryReader.ReadBytes((Int32)_TotalBytes); } byte[] signature = rsaKey.SignData(data, new SHA1CryptoServiceProvider()); File.WriteAllBytes(signFilePath, signature); return false; }
c# получить хэш файла / c# get hash from file
public static string GetMD5HashFromFile(string fileName) { FileStream file = new FileStream(fileName, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); }
c# читаем последнюю строку и удаляем её / Get last line from file and delete it
private static string ReadDeleteHashFromFile(string MyFilePath) { string lastline = ""; using (StreamReader reader = new StreamReader(dumpFilePath)) { using (StreamWriter writer = new StreamWriter("myfile.tmp")) { string line = reader.ReadLine(); while (!reader.EndOfStream) { writer.Write(line); line = reader.ReadLine(); if (!reader.EndOfStream) writer.Write(writer.NewLine); } lastline = line; } } File.Delete(MyFilePath); File.Move("myfile.tmp", MyFilePath); File.Delete("myfile.tmp"); return lastline; }
c# add a line to the end of a file
using (StreamWriter sw = new StreamWriter(dumpFilePath, true, Encoding.ASCII)) { string s="My new line"; sw.WriteLine(); sw.Write(s); sw.Close(); }
Subscribe to:
Posts (Atom)