Key ring can now be saved/loaded from files

This commit is contained in:
Kevin 2020-07-22 01:26:59 -05:00
parent a0617da1f5
commit 12a899d6bd
4 changed files with 130 additions and 5 deletions

80
tests/profile/testSave.cs Normal file
View File

@ -0,0 +1,80 @@
using NUnit.Framework;
using chestcrypto.identity;
using chestcrypto;
using chestcrypto.profile;
using keyring;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using Sodium;
using SimpleBase;
namespace testProfileSave
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestSaveMany(){
var temp = Path.GetTempPath();
List<PublicIdentity> pub = new List<PublicIdentity>();
List<PrivateIdentity> priva = new List<PrivateIdentity>();
KeyRing ring = new KeyRing();
DoublePublicKey key;
DoublePrivateKey privKey;
PublicIdentity iden;
PrivateIdentity priv;
var publicProfile = temp + "/public.keyring.csv";
var privateProfile = temp + "/private.keyring.csv";
string[] names = {"bob", "kevin", "sam", "joe", "sarah sara"};
for (int i = 0; i < 5; i++){
key = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
iden = new PublicIdentity(key, names[i]);
pub.Add(iden);
ring.addPublicIdentity(iden);
}
for (int i = 0; i < 5; i++){
privKey = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
priv = new PrivateIdentity(privKey, names[i]);
priva.Add(priv);
ring.addPrivateIdentity(priv);
}
KeyRingSave saver = new KeyRingSave(temp);
saver.save(ring);
string data = System.IO.File.ReadAllText(publicProfile);
foreach(string name in names){
Assert.IsTrue(data.Contains(name));
}
foreach(PublicIdentity id in pub){
if (! data.Contains(id.getEncodedKey())){
Assert.Fail();
}
if (! names.Contains(id.getName())){
Assert.Fail();
}
}
data = System.IO.File.ReadAllText(privateProfile);
foreach(PrivateIdentity id in priva){
if (! data.Contains(id.getEncodedKey())){
Assert.Fail();
}
if (! names.Contains(id.getName())){
Assert.Fail();
}
}
File.Delete(privateProfile);
File.Delete(publicProfile);
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>

View File

@ -1,6 +1,4 @@
using System.IO; using System.IO;
using System.Collections.Generic;
using System;
using keyring; using keyring;
using chestcrypto.identity; using chestcrypto.identity;

View File

@ -0,0 +1,47 @@
using System.IO;
using keyring;
using chestcrypto.identity;
namespace chestcrypto.profile{
public class KeyRingSave{
private string profileDir;
private string privateProfile;
private string publicProfile;
public KeyRingSave(string profileDirectory){
profileDir = profileDirectory;
if (! Directory.Exists(profileDir)){
Directory.CreateDirectory(profileDir); // Does not error if it exists already
}
privateProfile = profileDir + "/private.keyring.csv";
publicProfile = profileDir + "/public.keyring.csv";
}
public void save(KeyRing ring){
string header = "base85Key,name,note";
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(privateProfile, false))
{
foreach(PrivateIdentity iden in ring.privateIdentities){
file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
}
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(publicProfile, false))
{
foreach(PublicIdentity iden in ring.publicIdentities){
file.Write(header + "\r\n" + iden.getEncodedKey() + "," + iden.name + "," + iden.getNote() + "\r\n");
}
}
}
}
}