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");
        }
Beispiel #2
0
        public void TestGetSecretKey()
        {
            DeployKeyRings();

            Target.GetSecretKey(_secretKey).Should().Be(_secretKey, because: "Should get secret key using parsed id source");

            Target.GetSecretKey(_secretKey.UserID).Should().Be(_secretKey, because: "Should get secret key using user id");
            Target.GetSecretKey(_secretKey.FormatKeyID()).Should().Be(_secretKey, because: "Should get secret key using key id string");
            Target.GetSecretKey(_secretKey.FormatFingerprint()).Should().Be(_secretKey, because: "Should get secret key using fingerprint string");

            Target.GetSecretKey().Should().Be(_secretKey, because: "Should get default secret key");

            Target.Invoking(x => x.GetSecretKey("*****@*****.**")).ShouldThrow <KeyNotFoundException>();
        }
        public void TestGetSecretKey()
        {
            DeployKeyRings();

            OpenPgp.GetSecretKey(_secretKey).Should().Be(_secretKey, because: "Should get secret key using parsed id source");

            OpenPgp.GetSecretKey(_secretKey.UserID).Should().Be(_secretKey, because: "Should get secret key using user id");
            OpenPgp.GetSecretKey(_secretKey.FormatKeyID()).Should().Be(_secretKey, because: "Should get secret key using key id string");
            OpenPgp.GetSecretKey(_secretKey.FormatFingerprint()).Should().Be(_secretKey, because: "Should get secret key using fingerprint string");

            OpenPgp.GetSecretKey().Should().Be(_secretKey, because: "Should get default secret key");

            Assert.Throws <KeyNotFoundException>(() => OpenPgp.GetSecretKey("*****@*****.**"));
        }
Beispiel #4
0
        /// <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));
        }