This repository was archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
116 lines (107 loc) · 4.87 KB
/
Utils.cs
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
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NewVoiceMedia.Pci.Integration
{
public static class Utils
{
[Command("Writes new generated key pair to given paths")]
public static string GenerateKeys(string privateKeyPath = "private-key.xml",
string publicKeyPath = "public-key.xml")
{
var key = RSA.Create();
key.KeySize = PaymentHandler.KeySize;
File.WriteAllText(privateKeyPath, RsaKeyLoader.ToXmlString(key, includePrivateParameters: true));
File.WriteAllText(publicKeyPath, RsaKeyLoader.ToXmlString(key, includePrivateParameters: false));
return $"Files written to {Path.GetDirectoryName(Path.GetFullPath(privateKeyPath))}\n" +
$" {privateKeyPath}: keep this one secure and never pass to anyone\n" +
$" {publicKeyPath}: pass this one to NewVoiceMedia operations";
}
[Command("Checks whether given xml keys match each other (true) or not (false)")]
public static bool CheckKeys(
string privateKeyPath = "private-key.xml",
string publicKeyPath = "public-key.xml")
{
var privateKey = RsaKeyLoader.FromXmlFile(privateKeyPath);
var publicKey = RsaKeyLoader.FromXmlFile(publicKeyPath);
var testData = PaymentHandler.DigestEncoding.GetBytes("NewVoiceMedia Rulezzz");
var signature = privateKey.SignData(testData,
PaymentHandler.HashAlgorithm,
PaymentHandler.SignaturePadding);
return publicKey.VerifyData(testData,
signature,
PaymentHandler.HashAlgorithm,
PaymentHandler.SignaturePadding);
}
[Command("Signs given string with given private key and returns base64 encoded signature")]
public static string SignData(string data, string privateKeyPath = "private-key.xml")
{
var privateKey = RsaKeyLoader.FromXmlFile(privateKeyPath);
var signature = privateKey.SignData(PaymentHandler.DigestEncoding.GetBytes(data),
PaymentHandler.HashAlgorithm,
PaymentHandler.SignaturePadding);
return Convert.ToBase64String(signature);
}
[Command("Checks whether given signature matches given pair of data and public key (true) or not (false)")]
public static bool VerifySignature(string signatureBase64,
string data,
string publicKeyPath = "public-key.xml")
{
var publicKey = RsaKeyLoader.FromXmlFile(publicKeyPath);
var signature = Convert.FromBase64String(signatureBase64);
var dataBytes = PaymentHandler.DigestEncoding.GetBytes(data);
return publicKey.VerifyData(dataBytes,
signature,
PaymentHandler.HashAlgorithm,
PaymentHandler.SignaturePadding);
}
[Command("Convert RSA key from PEM to XML")]
public static void ConvertKey(string keyPath)
{
var newPath = Path.Combine(Path.GetDirectoryName(keyPath),
Path.GetFileNameWithoutExtension(keyPath)+".xml");
File.WriteAllText(newPath, PemToXmlConverter.Convert(File.OpenRead(keyPath)));
}
[Command("Sends payment request to server and returns response")]
public static async Task<string> SendRequest(
string accountKey,
PaymentHandler.Gateway gateway,
string payloadPath = "payload.xml",
string privateKeyPath = "private-key.xml",
string hostname = "paymentapi.contact-world.net")
{
var payload = File.ReadAllText(payloadPath).Trim();
var privateKey = RsaKeyLoader.FromXmlFile(privateKeyPath);
var handler = new PaymentHandler(accountKey, gateway, hostname, privateKey, TimeSpan.FromSeconds(1));
return await handler.MakePayment(payload, Console.WriteLine);
}
[Command("Relays payment request from standard input to server and returns response")]
public static async Task<string> RelayRequest(
string accountKey,
PaymentHandler.Gateway gateway,
string privateKeyPath = "private-key.xml",
string hostname = "paymentapi.contact-world.net")
{
var payload = new StringBuilder();
string line;
while ((line = Console.ReadLine()) != null)
payload.AppendLine(line);
var privateKey = RsaKeyLoader.FromXmlFile(privateKeyPath);
var handler = new PaymentHandler(accountKey, gateway, hostname, privateKey, TimeSpan.FromSeconds(1));
return await handler.MakePayment(payload.ToString(), Console.WriteLine);
}
[Command("Checks external IP address of current machine")]
public static string CheckIp(string provider = "checkip.dyndns.org")
{
using (var client = new HttpClient())
{
var response = client.GetStringAsync("http://" + provider).Result;
return Regex.Match(response, @"\d+\.\d+\.\d+\.\d+").Value;
}
}
}
}