summary refs log tree commit diff
path: root/crypto/test/src/openpgp/examples/PgpExampleUtilities.cs
blob: fd371d7af551b27dce40b3eab80d734ba40595c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.IO;

namespace Org.BouncyCastle.Bcpg.OpenPgp.Examples
{
	internal class PgpExampleUtilities
	{
		internal static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm)
		{
			MemoryStream bOut = new MemoryStream();
			PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
			PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Binary,
				new FileInfo(fileName));
			comData.Close();
			return bOut.ToArray();
		}

		/**
		 * Search a secret key ring collection for a secret key corresponding to keyID if it
		 * exists.
		 * 
		 * @param pgpSec a secret key ring collection.
		 * @param keyID keyID we want.
		 * @param pass passphrase to decrypt secret key with.
		 * @return
		 * @throws PGPException
		 * @throws NoSuchProviderException
		 */
		internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
		{
			PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID);

			if (pgpSecKey == null)
			{
				return null;
			}

			return pgpSecKey.ExtractPrivateKey(pass);
		}

		internal static PgpPublicKey ReadPublicKey(string fileName)
		{
			using (Stream keyIn = File.OpenRead(fileName))
			{
				return ReadPublicKey(keyIn);
			}
		}

		/**
		 * A simple routine that opens a key ring file and loads the first available key
		 * suitable for encryption.
		 * 
		 * @param input
		 * @return
		 * @throws IOException
		 * @throws PGPException
		 */
		internal static PgpPublicKey ReadPublicKey(Stream input)
		{
			PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(
				PgpUtilities.GetDecoderStream(input));

			//
			// we just loop through the collection till we find a key suitable for encryption, in the real
			// world you would probably want to be a bit smarter about this.
			//

			foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
			{
				foreach (PgpPublicKey key in keyRing.GetPublicKeys())
				{
					if (key.IsEncryptionKey)
					{
						return key;
					}
				}
			}

			throw new ArgumentException("Can't find encryption key in key ring.");
		}

		internal static PgpSecretKey ReadSecretKey(string fileName)
		{
			using (Stream keyIn = File.OpenRead(fileName))
			{
				return ReadSecretKey(keyIn);
			}
		}

		/**
		 * A simple routine that opens a key ring file and loads the first available key
		 * suitable for signature generation.
		 * 
		 * @param input stream to read the secret key ring collection from.
		 * @return a secret key.
		 * @throws IOException on a problem with using the input stream.
		 * @throws PGPException if there is an issue parsing the input stream.
		 */
		internal static PgpSecretKey ReadSecretKey(Stream input)
		{
			PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
				PgpUtilities.GetDecoderStream(input));

			//
			// we just loop through the collection till we find a key suitable for encryption, in the real
			// world you would probably want to be a bit smarter about this.
			//

			foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings())
			{
				foreach (PgpSecretKey key in keyRing.GetSecretKeys())
				{
					if (key.IsSigningKey)
					{
						return key;
					}
				}
			}

			throw new ArgumentException("Can't find signing key in key ring.");
		}
	}
}