/// <inheritdoc/> public byte[] Sign(byte[] data, OpenPgpSecretKey secretKey, string?passphrase = null) { #region Sanity checks if (data == null) { throw new ArgumentNullException(nameof(data)); } if (secretKey == null) { throw new ArgumentNullException(nameof(secretKey)); } #endregion var pgpSecretKey = SecretBundle.GetSecretKey(secretKey.KeyID); if (pgpSecretKey == null) { throw new KeyNotFoundException("Specified OpenPGP key not found on system"); } var pgpPrivateKey = GetPrivateKey(pgpSecretKey, passphrase); var signatureGenerator = new PgpSignatureGenerator(pgpSecretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1); signatureGenerator.InitSign(PgpSignature.BinaryDocument, pgpPrivateKey); signatureGenerator.Update(data); return(signatureGenerator.Generate().GetEncoded()); }
public void TestDeployPublicKey() { using var tempDir = new TemporaryDirectory("0install-unit-tests"); const string publicKey = "public"; var secretKey = new OpenPgpSecretKey(keyID: 123, fingerprint: new byte[] { 1, 2, 3 }, userID: "user"); var openPgpMock = CreateMock <IOpenPgp>(); openPgpMock.Setup(x => x.ExportKey(secretKey)).Returns(publicKey); openPgpMock.Object.DeployPublicKey(secretKey, tempDir.Path); File.ReadAllText(tempDir + Path.DirectorySeparatorChar + secretKey.FormatKeyID() + ".gpg") .Should().Be(publicKey, because: "Public key should be written to parallel file in directory"); }
/// <inheritdoc/> public byte[] Sign(byte[] data, OpenPgpSecretKey secretKey, string passphrase = null) { #region Sanity checks if (data == null) { throw new ArgumentNullException(nameof(data)); } if (secretKey == null) { throw new ArgumentNullException(nameof(secretKey)); } #endregion string output = new CliControl(HomeDir, data).Execute("--batch", "--no-secmem-warning", "--passphrase", passphrase ?? "", "--local-user", secretKey.FormatKeyID(), "--detach-sign", "--armor", "--output", "-", "-"); string signatureBase64 = output .GetRightPartAtFirstOccurrence(Environment.NewLine + Environment.NewLine) .GetLeftPartAtLastOccurrence(Environment.NewLine + "=") .Replace(Environment.NewLine, "\n"); return(Convert.FromBase64String(signatureBase64)); }