public static string EncryptPassword(string password) { byte[] passwordBytes = Encoding.UTF8.GetBytes(password); using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { sha256.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length); return(Encoding.UTF8.GetString(sha256.Hash)); } }
static void Main3(string[] args) { var buf = new byte[0]; var sha256 = new SHA256CryptoServiceProvider(); sha256.TransformFinalBlock(buf, 0, 0); var tester = new AutoTest(); tester.Test(); const string ftp_uri = "ftp://100.100.100.145/config/System Variables.sys"; var request = (FtpWebRequest)WebRequest.Create(ftp_uri); request.Credentials = new NetworkCredential("anon", "12345"); request.UseBinary = true; request.UsePassive = true; request.KeepAlive = false; request.Method = WebRequestMethods.Ftp.DownloadFile; var rsp = request.GetResponse(); Console.WriteLine("Got Response:"); using (var reader = new StreamReader(rsp.GetResponseStream())) { Console.WriteLine(reader.ReadToEnd()); } Console.WriteLine("\n\n\n\n\n"); Console.WriteLine("Absolute File URI"); var uri = new Uri("C:\\foo\\bar.txt"); ShowProps(uri); //Console.WriteLine("Relative File URI"); //var uri3 = new Uri("\\foo\\..\\..\\bar.txt", UriKind.Absolute); //ShowProps(uri3); Console.WriteLine("\nFTP URI"); var uri2 = new Uri(ftp_uri.ToUpper()); ShowProps(uri2); Console.WriteLine("\n\n\n\n\n"); var eng = new FileVerifier.FVEngine(); var cs = eng.AddFileAuto(ftp_uri, "C:\\ftp\\out.txt"); Console.WriteLine(cs); foreach (var item in eng.GetData()) { Console.WriteLine($"{item.Key}: {item.Value}"); } }
public string GenerateHash() { string Reason = "Motivo"; string Location = "Localização"; string Contact = "Contato"; string signatureFieldName = null; appearance.SetVisibleSignature(new Rectangle(500, 150, 400, 200), 1, signatureFieldName); appearance.SignDate = DateTime.Now; appearance.Reason = Reason; appearance.Location = Location; appearance.Contact = Contact; StringBuilder buf = new StringBuilder(); buf.Append("Digitally signed by"); buf.Append("\n"); buf.Append(userName); buf.Append("\n"); buf.Append("Date: " + appearance.SignDate); appearance.Layer2Text = buf.ToString(); appearance.Acro6Layers = true; appearance.CertificationLevel = 0; PdfSignature dic = GeneratePdfSignature(); appearance.CryptoDictionary = dic; Dictionary <PdfName, int> exclusionSizes = new Dictionary <PdfName, int>(); exclusionSizes.Add(PdfName.CONTENTS, (RESERVED_SPACE_SIGNATURE * 2) + 2); appearance.PreClose(exclusionSizes); HashAlgorithm sha = new SHA256CryptoServiceProvider(); Stream s = appearance.GetRangeStream(); int read = 0; byte[] buff = new byte[0x2000]; while ((read = s.Read(buff, 0, 0x2000)) > 0) { sha.TransformBlock(buff, 0, read, buff, 0); } sha.TransformFinalBlock(buff, 0, 0); StringBuilder hex = new StringBuilder(sha.Hash.Length * 2); foreach (byte b in sha.Hash) { hex.AppendFormat("{0:x2}", b); } return(hex.ToString()); }
private string Gethash(string name) { // 進行状況を表示するための設定や変数 int percent = 0; // ファイルをUTF-8エンコードでバイト配列化 byte[] byteValue = File.ReadAllBytes(name); // SHA256のハッシュ値を取得する using (SHA256 crypto = new SHA256CryptoServiceProvider()) { int length = byteValue.Length; decimal doneSize = 0; for (int offset = 0; offset < length; offset += blockSize) { if (offset + blockSize < length) { crypto.TransformBlock(byteValue, offset, blockSize, null, 0); doneSize = offset + blockSize; } else { crypto.TransformFinalBlock(byteValue, offset, length - offset); doneSize = length; } if (percent != doneSize * 100 / length) { percent = (int)(doneSize * 100 / length); Dispatcher.Invoke(() => { dl_ProgressBar.Value = percent; dl_progress_label.Content = $"{percent}%"; }); } } byte[] hashValue = crypto.Hash; StringBuilder hashedText = new StringBuilder(); for (int i = 0; i < hashValue.Length; i++) { hashedText.AppendFormat("{0:X2}", hashValue[i]); } return(hashedText.ToString()); } }
//schimbare functie pentru iText private string generateHash() { appearance.SetVisibleSignature(new Rectangle(500, 150, 400, 200), 1, "signature"); appearance.SignDate = DateTime.Now; appearance.Reason = "Test Licenta"; appearance.Location = "Bucuresti"; appearance.Contact = "mta"; StringBuilder buf = new StringBuilder(); buf.Append("Semnat digital de"); buf.Append("\n"); buf.Append(userName); buf.Append("\n"); buf.Append("Date: " + appearance.SignDate); appearance.Layer2Text = buf.ToString(); appearance.Acro6Layers = true; appearance.CertificationLevel = 0; PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED) { Date = new PdfDate(appearance.SignDate), Name = userName }; dic.Reason = appearance.Reason; dic.Location = appearance.Location; dic.Contact = appearance.Contact; appearance.CryptoDictionary = dic; Dictionary <PdfName, int> exclusionSizes = new Dictionary <PdfName, int>(); exclusionSizes.Add(PdfName.CONTENTS, (csize * 2) + 2); appearance.PreClose(exclusionSizes); HashAlgorithm sha = new SHA256CryptoServiceProvider(); Stream s = appearance.GetRangeStream(); int read = 0; byte[] buff = new byte[0x2000]; while ((read = s.Read(buff, 0, 0x2000)) > 0) { sha.TransformBlock(buff, 0, read, buff, 0); } sha.TransformFinalBlock(buff, 0, 0); return(System.Convert.ToBase64String(sha.Hash)); }
private void ButtonTransformFinalBlock_Click(object sender, EventArgs e) { byte[] data = Encoding.UTF8.GetBytes(textBoxLastInputData.Text); sha256.TransformFinalBlock(data, 0, data.Length); textBoxHashBytes.Text = BitConverter.ToString(sha256.Hash); if (textBoxHashBytes.Text != BitConverter.ToString(ComputeHashTest())) { MessageBox.Show("Error!"); } else { MessageBox.Show("Hash identicos"); } sha256.Dispose(); }