1 module libsweatyballs.security.identity;
2 
3 import crypto.rsa : RSAKeyPair, RSA;
4 
5 /**
6 * Identity
7 *
8 * Description: Represents this router's identity
9 */
10 public final class Identity
11 {
12     /**
13     * Router identity
14     */
15     private RSAKeyPair rsaKeys;
16     private string fingerprint;
17 
18     this(RSAKeyPair keys)
19     {
20         /* ToDO: Validate keys */
21         validateKeys(keys);
22 
23         this.rsaKeys = keys;
24 
25         /* Generate fingerprint */
26         fingerprint = generateFingerprint(rsaKeys);
27     }
28 
29     /**
30     * Generates a fingerprint of the public key
31     *
32     * This will run SHA512 on the public key
33     */
34     public static string generateFingerprint(RSAKeyPair keys)
35     {
36         /* Generated fingerprint */
37         string fingerprint;
38 
39         /* TODO: Validate keys */
40         validateKeys(keys);
41 
42         /* SHA-512 the public key */
43         import std.digest.sha;
44         byte[] dataIn = [1,2];
45         ubyte[] shaBytes = sha512Of(keys.publicKey);
46         fingerprint = toHexString(shaBytes);
47 
48 
49         /* TODO: Return fingerprint */
50         return fingerprint;
51     }
52 
53     public static bool validateKeys(RSAKeyPair keys)
54     {
55         /* TODO: make sure non-empty and that they are related */
56         return true;
57     }
58 
59 
60     /**
61     * Creates a new router identity. This includes generating a new
62     * set of the following:
63     * 1. An RSA key-pair
64     * 2. TODO
65     *
66     * @param uint rsaBitLength: This is the bit length of the RSA keys
67     */
68     public static Identity newIdentity(uint rsaBitLength)
69     {
70         Identity identity;
71 
72         /* Create new RSA keys */
73         RSAKeyPair rsaKeys = RSA.generateKeyPair(rsaBitLength);
74 
75         /* Create the Identity with the given keypair */
76         identity = new Identity(rsaKeys);
77 
78         return identity;
79     }
80 
81     public RSAKeyPair getKeys()
82     {
83         return rsaKeys;
84     }
85 
86     public string getFingerprint()
87     {
88         return fingerprint;
89     }
90 
91     public override string toString()
92     {
93         return "Identity "~fingerprint~")";
94     }
95 }
96 
97 unittest
98 {
99     import std.stdio;
100 
101     Identity identity = Identity.newIdentity(1024);
102 
103     writeln(identity);
104     writeln(identity.getFingerprint());
105     writeln(identity.getKeys());
106 }