private static byte[] RevokePublicKey(PgpSecretKey sKey, char[] sPass, PgpPublicKey keyToSign, bool armour) { Stream os = new MemoryStream(); if (armour) { os = new ArmoredOutputStream(os); } PgpPrivateKey privKey = sKey.ExtractPrivateKey(sPass); PgpSignatureGenerator sGen = new PgpSignatureGenerator(sKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1); sGen.InitSign(PgpSignature.KeyRevocation, privKey); BcpgOutputStream bOut = new BcpgOutputStream(os); sGen.GenerateOnePassVersion(false).Encode(bOut); PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); spGen.SetRevocable(false, true); DateTime baseDate = new DateTime(1970, 1, 1); TimeSpan tSpan = DateTime.UtcNow - baseDate; spGen.SetSignatureExpirationTime(false, tSpan.Seconds); PgpSignatureSubpacketVector packetVector = spGen.Generate(); sGen.SetHashedSubpackets(packetVector); bOut.Flush(); if (armour) { os.Close(); } return(PgpPublicKey.AddCertification(keyToSign, sGen.Generate()).GetEncoded()); }
/* * Helper for above. */ static byte [] SignPublicKey( PgpSecretKey secretKey, string password, PgpPublicKey keyToBeSigned, bool isCertain) { // Extracting private key, and getting ready to create a signature. PgpPrivateKey pgpPrivKey = secretKey.ExtractPrivateKey(password.ToCharArray()); PgpSignatureGenerator sGen = new PgpSignatureGenerator(secretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1); sGen.InitSign(isCertain ? PgpSignature.PositiveCertification : PgpSignature.CasualCertification, pgpPrivKey); // Creating a stream to wrap the results of operation. Stream os = new MemoryStream(); BcpgOutputStream bOut = new BcpgOutputStream(os); sGen.GenerateOnePassVersion(false).Encode(bOut); // Creating a generator. PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); PgpSignatureSubpacketVector packetVector = spGen.Generate(); sGen.SetHashedSubpackets(packetVector); bOut.Flush(); // Returning the signed public key. return(PgpPublicKey.AddCertification(keyToBeSigned, sGen.Generate()).GetEncoded()); }
public JObject ToSignedJSON(PgpSecretKey SecretKey, String Passphrase) { var SignatureGenerator = new PgpSignatureGenerator(SecretKey.PublicKey.Algorithm, HashAlgorithms.Sha512); SignatureGenerator.InitSign(PgpSignatureTypes.BinaryDocument, SecretKey.ExtractPrivateKey(Passphrase)); var JSON = ToJSON(); var JSONText = JSONWhitespaceRegEx.Replace(JSON.ToString().Replace(Environment.NewLine, " "), " ").Trim(); var JSONBlob = JSON.ToUTF8Bytes(); SignatureGenerator.Update(JSONBlob, 0, JSONBlob.Length); var SignatureGen = SignatureGenerator.Generate(); var OutputStream = new MemoryStream(); var SignatureStream = new BcpgOutputStream(new ArmoredOutputStream(OutputStream)); SignatureGen.Encode(SignatureStream); SignatureStream.Flush(); SignatureStream.Close(); OutputStream.Flush(); OutputStream.Close(); var Signature = OutputStream.ToArray().ToHexString(); _Signatures.Add(Signature); JSON["signature"] = Signature; return(JSON); }
/// <summary>Close the compressed object.</summary>summary> public void Close() { if (dOut != null) { //switch (algorithm) //{ // case CompressionAlgorithmTag.BZip2: // // TODO No Finish method on BZip2OutputStream // break; // case CompressionAlgorithmTag.Zip: // case CompressionAlgorithmTag.ZLib: // ((DeflaterOutputStream) dOut).Finish(); // break; //} //dOut.Flush(); // TODO IgnoreClose stuff is a workaround for BZip2OutputStream problem above pkOut.IgnoreClose = true; dOut.Close(); pkOut.IgnoreClose = false; pkOut.Finish(); pkOut.Flush(); dOut = null; pkOut = null; } }
/// <summary>Close the compressed object.</summary>summary> public void Close() { if (dOut != null) { switch (algorithm) { case CompressionAlgorithmTag.BZip2: ((CBZip2OutputStream)dOut).Finish(); break; case CompressionAlgorithmTag.Zip: case CompressionAlgorithmTag.ZLib: ((ZDeflaterOutputStream)dOut).Finish(); break; } dOut.Flush(); pkOut.Finish(); pkOut.Flush(); dOut = null; pkOut = null; } }
public void Close() { if (cOut != null) { if (digestOut != null) { BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(digestOut, PacketTag.ModificationDetectionCode, 20L); bcpgOutputStream.Flush(); digestOut.Flush(); byte[] array = DigestUtilities.DoFinal(digestOut.WriteDigest()); cOut.Write(array, 0, array.Length); } cOut.Flush(); try { pOut.Write(c.DoFinal()); pOut.Finish(); } catch (Exception ex) { throw new IOException(ex.Message, ex); } cOut = null; pOut = null; } }
/// <summary> /// Close the literal data packet - this is equivalent to calling Close() /// on the stream returned by the Open() method. /// </summary> public void Close() { if (pkOut != null) { pkOut.Finish(); pkOut.Flush(); pkOut = null; } }
/// <summary> /// Close the literal data packet - this is equivalent to calling Close() /// on the stream returned by the Open() method. /// </summary> public void Close() { if (_pkOut != null) { _pkOut.Finish(); _pkOut.Flush(); _pkOut = null; } }
/// <summary> /// Close the literal data packet - this is equivalent to calling Close() /// on the stream returned by the Open() method. /// </summary> public void Close() { if (_pkOut == null) { return; } _pkOut.Finish(); _pkOut.Flush(); _pkOut = null; }
public void Close() { if (dOut != null) { if (dOut != pkOut) { Platform.Dispose(dOut); } dOut = null; pkOut.Finish(); pkOut.Flush(); pkOut = null; } }
/// <summary>Close the compressed object.</summary> /// summary> public void Close() { if (_dOut != null) { if (_dOut != _pkOut) { _dOut.Flush(); _dOut.Dispose(); } _dOut = null; _pkOut.Finish(); _pkOut.Flush(); _pkOut = null; } }
private static byte[] SignPublicKey( PgpSecretKey secretKey, string secretKeyPass, PgpPublicKey keyToBeSigned, string notationName, string notationValue, bool armor) { Stream os = new MemoryStream(); if (armor) { os = new ArmoredOutputStream(os); } PgpPrivateKey pgpPrivKey = secretKey.ExtractPrivateKey( secretKeyPass.ToCharArray()); PgpSignatureGenerator sGen = new PgpSignatureGenerator( secretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1); sGen.InitSign(PgpSignature.DirectKey, pgpPrivKey); BcpgOutputStream bOut = new BcpgOutputStream(os); sGen.GenerateOnePassVersion(false).Encode(bOut); PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); bool isHumanReadable = true; spGen.SetNotationData(true, isHumanReadable, notationName, notationValue); PgpSignatureSubpacketVector packetVector = spGen.Generate(); sGen.SetHashedSubpackets(packetVector); bOut.Flush(); if (armor) { os.Close(); } return(PgpPublicKey.AddCertification(keyToBeSigned, sGen.Generate()).GetEncoded()); }
/// <summary> /// <p> /// Close off the encrypted object - this is equivalent to calling Close() on the stream /// returned by the Open() method. /// </p> /// <p> /// <b>Note</b>: This does not close the underlying output stream, only the stream on top of /// it created by the Open() method. /// </p> /// </summary> public void Close() { if (_cOut == null) { return; } // TODO Should this all be under the try/catch block? if (_digestOut != null) { // // hand code a mod detection packet // var bOut = new BcpgOutputStream( _digestOut, PacketTag.ModificationDetectionCode, 20); bOut.Flush(); _digestOut.Flush(); // TODO var dig = DigestUtilities.DoFinal(_digestOut.WriteDigest()); _cOut.Write(dig, 0, dig.Length); } _cOut.Flush(); try { _pOut.Write(_c.DoFinal()); _pOut.Finish(); } catch (Exception e) { throw new IOException(e.Message, e); } _cOut = null; _pOut = null; }
/// <summary> /// <p> /// Close off the encrypted object - this is equivalent to calling Close() on the stream /// returned by the Open() method. /// </p> /// <p> /// <b>Note</b>: This does not close the underlying output stream, only the stream on top of /// it created by the Open() method. /// </p> /// </summary> public void Close() { if (cOut != null) { // TODO Should this all be under the try/catch block? if (digestOut != null) { // // hand code a mod detection packet // BcpgOutputStream bOut = new BcpgOutputStream( digestOut, PacketTag.ModificationDetectionCode, 20); bOut.Flush(); digestOut.Flush(); // TODO byte[] dig = DigestUtilities.DoFinal(digestOut.WriteDigest()); cOut.Write(dig, 0, dig.Length); } cOut.Flush(); try { pOut.Write(c.DoFinal()); pOut.Finish(); } catch (Exception e) { throw new IOException(e.Message, e); } cOut = null; pOut = null; } }
/// <summary> /// Sign public key with secret key. To access the private key from the /// secret container a password needs to be provided. /// </summary> /// <param name="secretKey"> /// The secret key containing the private key for signing the public /// key. /// </param> /// <param name="password"> /// The password of the secret key. /// </param> /// <param name="keyToBeSigned"> /// The public key to be signed. /// </param> /// <param name="certain"> /// Flag indicating whether or not the certification is positive or just /// casual. /// </param> /// <returns> /// Returns the <see cref="PgpPublicKey"/> adorned with a signature by the /// private key passed in. /// </returns> public static PgpPublicKey SignPublicKey( PgpSecretKey secretKey, string password, PgpPublicKey keyToBeSigned, bool certain) { var id = keyToBeSigned.GetUserIds().Cast <string>().FirstOrDefault(); // Extracting private key, and getting ready to create a signature. var privateKey = secretKey.ExtractPrivateKey(password.ToCharArray()); var signatureGenerator = new PgpSignatureGenerator(secretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha256); signatureGenerator.InitSign( certain ? PgpSignature.PositiveCertification : PgpSignature.CasualCertification, privateKey); // Creating a stream to wrap the results of operation. var outputStream = new MemoryStream(); var packetOutputStream = new BcpgOutputStream(outputStream); signatureGenerator.GenerateOnePassVersion(false).Encode(packetOutputStream); // Creating a generator. var subpacketSignatureGenerator = new PgpSignatureSubpacketGenerator(); subpacketSignatureGenerator.SetSignerUserId(false, id); var packetVector = subpacketSignatureGenerator.Generate(); signatureGenerator.SetHashedSubpackets(packetVector); packetOutputStream.Flush(); // Returning the signed public key. return(PgpPublicKey.AddCertification( keyToBeSigned, id, signatureGenerator.GenerateCertification(id, keyToBeSigned))); }