aboutsummaryrefslogtreecommitdiff
path: root/Tools/Hazel-Networking/Hazel.UnitTests/Utils.cs
blob: df62b80dddb1b66c5e45f13bcea2d4ee6e2db495 (plain)
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
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Hazel.UnitTests
{
    static class Utils
    {
        /// <summary>
        /// Hex encode a byte array (lower case)
        /// </summary>
        public static string BytesToHex(byte[] data)
        {
            string chars = "0123456789abcdef";

            StringBuilder sb = new StringBuilder(data.Length * 2);
            for (int ii = 0, nn = data.Length; ii != nn; ++ii)
            {
                sb.Append(chars[data[ii] >> 4]);
                sb.Append(chars[data[ii] & 0xF]);
            }

            return sb.ToString().ToLower();
        }

        /// <summary>
        /// Decode a hex string to a byte array (lowercase)
        /// </summary>
        public static byte[] HexToBytes(string hex)
        {
            hex = hex.ToLower();
            hex = hex = string.Concat(hex.Where(c => !char.IsWhiteSpace(c)));

            byte[] output = new byte[hex.Length / 2];

            for (int ii = 0; ii != hex.Length; ++ii)
            {
                byte nibble;

                char c = hex[ii];
                if (c >= 'a')
                {
                    nibble = (byte)(0x0A + c - 'a');
                }
                else
                {
                    nibble = (byte)(c - '0');
                }

                if ((ii & 1) == 0)
                {
                    output[ii / 2] = (byte)(nibble << 4);
                }
                else
                {
                    output[ii / 2] |= nibble;
                }
            }

            return output;
        }

        public static byte[] DecodePEM(string pemData)
        {
            List<byte> result = new List<byte>();

            pemData = pemData.Replace("\r", "");
            string[] lines = pemData.Split('\n');
            foreach (string line in lines)
            {
                if (line.StartsWith("-----"))
                {
                    continue;
                }

                byte[] lineData = Convert.FromBase64String(line);
                result.AddRange(lineData);
            }

            return result.ToArray();
        }

        public static RSA DecodeRSAKeyFromPEM(string pemData)
        {
            var pemReader = new PemReader(new StringReader(pemData));
            var parameters = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)pemReader.ReadObject());
            var rsa = RSA.Create();
            rsa.ImportParameters(parameters);
            return rsa;
        }
    }
}