From a0617da1f589616749caa1c215f6c2b810756ec0 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 8 Jun 2020 02:59:43 -0500 Subject: [PATCH] finished restore test --- tests/profile/testLoad.cs | 86 +++++++++++++++++++---- treasurechest/identity/private.cs | 3 +- treasurechest/identity/public.cs | 11 ++- treasurechest/keyring/keyring.cs | 4 +- treasurechest/profile/restoreKeyring.cs | 93 ++++++++++++++++++++++++- 5 files changed, 174 insertions(+), 23 deletions(-) diff --git a/tests/profile/testLoad.cs b/tests/profile/testLoad.cs index 48a7425..883e721 100644 --- a/tests/profile/testLoad.cs +++ b/tests/profile/testLoad.cs @@ -9,7 +9,7 @@ using System.Linq; using System.Collections.Generic; using System.Globalization; using Sodium; -using CsvHelper; +using SimpleBase; namespace testProfileLoad { @@ -21,32 +21,90 @@ namespace testProfileLoad } [Test] - public void TestLoad(){ + public void TestLoadMany(){ + string header = "base85Key,name,note"; + var temp = Path.GetTempPath(); + List pub = new List(); + KeyRing ring = new KeyRing(); + DoublePublicKey key; + PublicIdentity iden; + var publicProfile = temp + "/public.keyring.csv"; + var privateProfile = temp + "/private.keyring.csv"; + + using (System.IO.StreamWriter file = + new System.IO.StreamWriter(publicProfile, false)) + { + file.Write(header + "\r\n"); + } + + for (int i = 0; i < 10; i++){ + key = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey); + iden = new PublicIdentity(key, "test"); + ring.addPublicIdentity(iden); + pub.Add(iden); + using (StreamWriter w = File.AppendText(publicProfile)) + { + w.WriteLine(iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n"); + } + } + KeyRing restored = new RestoreKeyring(temp).getKeyring(); + + Assert.IsTrue(restored.publicIdentities.Count > 0); + bool ran = false; + foreach(PublicIdentity id in restored.publicIdentities){ + ran = false; + foreach(PublicIdentity existing in pub){ + if (Enumerable.SequenceEqual(id.getPublicKey().getRawDouble(), existing.getPublicKey().getRawDouble())){ + ran = true; + break; + } + } + Assert.IsTrue(ran); + } + File.Delete(publicProfile); + } + + [Test] + public void TestLoadSingleEntries(){ var temp = Path.GetTempPath(); var publicProfile = temp + "/public.keyring.csv"; var privateProfile = temp + "/private.keyring.csv"; DoublePrivateKey key = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey); PrivateIdentity iden = new PrivateIdentity(key, "bob"); - DoublePrivateKey key2 = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey); - PrivateIdentity iden2 = new PrivateIdentity(key2, "alice"); + DoublePublicKey key2 = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey); + PublicIdentity iden2 = new PublicIdentity(key2, "alice"); KeyRing ring = new KeyRing(); ring.addPrivateIdentity(iden); + ring.addPublicIdentity(iden2); + string header = "base85Key,name,note"; - using (var writer = new StreamWriter(privateProfile)) - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + using (System.IO.StreamWriter file = + new System.IO.StreamWriter(privateProfile, false)) { - csv.WriteRecords(ring.privateIdentities); - } - using (var writer = new StreamWriter(publicProfile)) - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) - { - csv.WriteRecords(ring.publicIdentities); + file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n"); } - KeyRing restored = new RestoreKeyring(temp); + using (System.IO.StreamWriter file = + new System.IO.StreamWriter(publicProfile, false)) + { + file.Write(header + "\r\n" + iden2.getEncodedKey() + "," + iden2.name + "," + iden2.getNote() + "\r\n"); + } + + + KeyRing restored = new RestoreKeyring(temp).getKeyring(); + Assert.IsTrue(restored.privateIdentities.Count > 0); + Assert.IsTrue(restored.publicIdentities.Count > 0); + foreach(PrivateIdentity id in restored.privateIdentities){ + Assert.IsTrue(Enumerable.SequenceEqual(id.getPrivateKey().getRawDouble(), iden.getPrivateKey().getRawDouble())); + } + foreach(PublicIdentity id in restored.publicIdentities){ + Assert.IsTrue(Enumerable.SequenceEqual(id.getPublicKey().getRawDouble(), iden2.getPublicKey().getRawDouble())); + } + + File.Delete(privateProfile); + File.Delete(publicProfile); - Directory.Delete(privateProfile); } diff --git a/treasurechest/identity/private.cs b/treasurechest/identity/private.cs index 16ec36b..02be729 100644 --- a/treasurechest/identity/private.cs +++ b/treasurechest/identity/private.cs @@ -6,14 +6,13 @@ namespace chestcrypto.identity /* PrivateIdentity is a wrapper around a DoublePrivateKey providing associated metadata such as alias and note */ - private DoublePrivateKey key { get; set; } + private DoublePrivateKey key; public string base85Key { get { return getEncodedKey(); }} public string name { get; set; } public string comment { get; set; } // human's note - public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){ key = doublePrivateKey; name = alias; diff --git a/treasurechest/identity/public.cs b/treasurechest/identity/public.cs index 9c71972..34b408d 100644 --- a/treasurechest/identity/public.cs +++ b/treasurechest/identity/public.cs @@ -6,8 +6,11 @@ namespace chestcrypto.identity PublicIdentity is a wrapper around a DoublePublicKey providing associated metadata such as alias and note */ private DoublePublicKey key; - private string name; - private string comment; // human's note + public string base85Key { get { + return getEncodedKey(); + }} + public string name { get; set; } + public string comment { get; set; } // human's note public PublicIdentity(DoublePublicKey doublePublicKey, string alias){ key = doublePublicKey; @@ -24,7 +27,9 @@ namespace chestcrypto.identity public DoublePublicKey getPublicKey(){return key;} public string getName(){return name;} public string getNote(){return comment;} - + public string getEncodedKey(){ + return SimpleBase.Base85.Z85.Encode(key.getRawDouble()); + } } diff --git a/treasurechest/keyring/keyring.cs b/treasurechest/keyring/keyring.cs index 56ce7ae..ff3b304 100644 --- a/treasurechest/keyring/keyring.cs +++ b/treasurechest/keyring/keyring.cs @@ -17,7 +17,7 @@ namespace keyring{ public void addPublicIdentity(PublicIdentity newIden){ foreach(PublicIdentity iden in publicIdentities){ - if (Enumerable.ReferenceEquals(iden.getPublicKey(), iden)){ + if (Enumerable.SequenceEqual(iden.getPublicKey().getRawDouble(), newIden.getPublicKey().getRawDouble())){ throw new DuplicateIdentityException(); } } @@ -25,7 +25,7 @@ namespace keyring{ } public void addPrivateIdentity(PrivateIdentity newIden){ foreach(PrivateIdentity iden in privateIdentities){ - if (Enumerable.ReferenceEquals(iden.getPrivateKey(), iden)){ + if (Enumerable.SequenceEqual(iden.getPrivateKey().getRawDouble(), newIden.getPrivateKey().getRawDouble())){ throw new DuplicateIdentityException(); } } diff --git a/treasurechest/profile/restoreKeyring.cs b/treasurechest/profile/restoreKeyring.cs index 27204f3..0c456fc 100644 --- a/treasurechest/profile/restoreKeyring.cs +++ b/treasurechest/profile/restoreKeyring.cs @@ -1,6 +1,8 @@ using System.IO; - +using System.Collections.Generic; +using System; using keyring; +using chestcrypto.identity; namespace chestcrypto.profile{ @@ -14,10 +16,97 @@ namespace chestcrypto.profile{ if (! Directory.Exists(profileDir)){ Directory.CreateDirectory(profileDir); // Does not error if it exists already } + } - private void getKeyring(){ + private PrivateIdentity getPrivateIdentityFromLine(string data){ + var parts = data.Split(','); + int counter = 0; + + DoublePrivateKey key = null; + string alias = ""; + string note = ""; + + foreach (string part in parts){ + switch(counter){ + case 0: + key = new DoublePrivateKey(SimpleBase.Base85.Z85.Decode(part).ToArray()); + break; + case 1: + alias = part; + break; + case 2: + note = part; + break; + default: + throw new InvalidDataException(); + } + counter += 1; + } + return new PrivateIdentity(key, alias, note); + } + private PublicIdentity getPublicIdentityFromLine(string data){ + var parts = data.Split(','); + int counter = 0; + + DoublePublicKey key = null; + string alias = ""; + string note = ""; + + foreach (string part in parts){ + switch(counter){ + case 0: + key = new DoublePublicKey(SimpleBase.Base85.Z85.Decode(part).ToArray()); + break; + case 1: + alias = part; + break; + case 2: + note = part; + break; + default: + throw new InvalidDataException(); + } + counter += 1; + } + return new PublicIdentity(key, alias, note); + } + public KeyRing getKeyring(){ KeyRing keyRing = new KeyRing(); + string[] lines; + bool first; + try{ + lines = System.IO.File.ReadAllLines(profileDir + "/private.keyring.csv"); + first = true; + foreach (string line in lines){ + if (first){ + first = false; + continue; + } + if (line.Length <= 1){ + continue; + } + keyRing.addPrivateIdentity(getPrivateIdentityFromLine(line)); + } + } + catch(System.IO.FileNotFoundException){} + try{ + lines = System.IO.File.ReadAllLines(profileDir + "/public.keyring.csv"); + first = true; + foreach (string line in lines){ + if (first){ + first = false; + continue; + } + if (line.Length <= 1){ + continue; + } + keyRing.addPublicIdentity(getPublicIdentityFromLine(line)); + } + } + catch(System.IO.FileNotFoundException){} + + return keyRing; } }