public void GenerateMessage() { string data = "hello world!"; byte[] dataBytes = Encoding.ASCII.GetBytes(data); DateTime testDateTime = new DateTime(1973, 7, 27); MemoryStream bOut = new MemoryStream(); var messageGenerator = new PgpMessageGenerator(bOut); using (var compressedGenerator = messageGenerator.CreateCompressed(PgpCompressionAlgorithm.Zip)) using (var signingGenerator = compressedGenerator.CreateSigned(PgpSignatureType.BinaryDocument, pgpPrivKey, PgpHashAlgorithm.Sha1)) { signingGenerator.HashedAttributes.SetSignerUserId(true, secretKey.GetUserIds().First().UserId); using (var literalStream = signingGenerator.CreateLiteral(PgpDataFormat.Binary, "_CONSOLE", testDateTime)) { literalStream.Write(dataBytes); } } bOut.Position = 0; var compressedMessage = (PgpCompressedMessage)PgpMessage.ReadMessage(bOut); var signedMessage = (PgpSignedMessage)compressedMessage.ReadMessage(); var literalMessage = (PgpLiteralMessage)signedMessage.ReadMessage(); literalMessage.GetStream().CopyTo(Stream.Null); Assert.IsTrue(signedMessage.Verify(pubKey)); }
public void GenerateTest(string type, string message) { PgpSecretKey pgpSecKey = ReadSecretKey(new MemoryStream(secretKey)); PgpPrivateKey pgpPrivKey = pgpSecKey.ExtractPrivateKey(""); MemoryStream bOut = new MemoryStream(); using (var messageGenerator = new PgpMessageGenerator(new ArmoredPacketWriter(bOut))) using (var signedGenerator = messageGenerator.CreateSigned(PgpSignatureType.CanonicalTextDocument, pgpPrivKey, PgpHashAlgorithm.Sha256)) { signedGenerator.HashedAttributes.SetSignerUserId(false, pgpSecKey.GetUserIds().First().UserId); using (var literalStream = signedGenerator.CreateLiteral(PgpDataFormat.Text, "", DateTime.MinValue)) { literalStream.Write(Encoding.UTF8.GetBytes(message)); } } byte[] bs = bOut.ToArray(); MessageTest(type, Encoding.ASCII.GetString(bs, 0, bs.Length)); }