Key ring can now be saved/loaded from files
This commit is contained in:
parent
a0617da1f5
commit
12a899d6bd
80
tests/profile/testSave.cs
Normal file
80
tests/profile/testSave.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
@ -12,8 +12,8 @@
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\treasurechest\treasurechest.csproj" />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\treasurechest\treasurechest.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using keyring;
|
||||
using chestcrypto.identity;
|
||||
|
||||
|
47
treasurechest/profile/saveKeyring.cs
Normal file
47
treasurechest/profile/saveKeyring.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user