-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEnvelope.cs
More file actions
66 lines (54 loc) · 2.39 KB
/
Copy pathEnvelope.cs
File metadata and controls
66 lines (54 loc) · 2.39 KB
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
using Jose;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.IO;
namespace MetadataRegistryExample
{
public class Envelope
{
[JsonProperty(PropertyName = "envelope_type")]
public string EnvelopeType { get; set; }
[JsonProperty(PropertyName = "envelope_version")]
public string EnvelopeVersion { get; set; }
[JsonProperty(PropertyName = "envelope_community")]
public string EnvelopeCommunity { get; set; }
[JsonProperty(PropertyName = "resource")]
public string Resource { get; set; }
[JsonProperty(PropertyName = "resource_format")]
public string ResourceFormat { get; set; }
[JsonProperty(PropertyName = "resource_encoding")]
public string ResourceEncoding { get; set; }
[JsonProperty(PropertyName = "resource_public_key")]
public string ResourcePublicKey { get; set; }
/// <summary>
/// Creates a MetadataRegistry envelope from an RSA key pair.
/// </summary>
/// <param name="publicKeyPath">Path to the public key file in the PEM format.</param>
/// <param name="secretKeyPath">Path to the private key file in the PEM format.</param>
/// <param name="contents">Envelope payload.</param>
/// <returns>An Envelope that can be serialized and POST'ed to a MetadataRegistry server.</returns>
public static Envelope CreateEnvelope(string publicKeyPath, string secretKeyPath, string contents)
{
RsaPrivateCrtKeyParameters privateKey;
using (var reader = File.OpenText(secretKeyPath))
{
privateKey = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)new PemReader(reader).ReadObject()).Private;
}
string publicKey = File.ReadAllText(publicKeyPath);
string encoded = JWT.Encode(contents, DotNetUtilities.ToRSA(privateKey), JwsAlgorithm.RS256);
return new Envelope
{
EnvelopeType = "resource_data",
EnvelopeVersion = "1.0.0",
EnvelopeCommunity = "ce_registry",
Resource = encoded,
ResourceFormat = "json",
ResourceEncoding = "jwt",
ResourcePublicKey = publicKey
};
}
}
}