Support for Binary Keys

This commit is contained in:
Jim Norman 2005-12-09 16:16:58 +00:00
parent 8b8d606aa3
commit c30b2b3195
5 changed files with 73 additions and 4 deletions

View File

@ -8,6 +8,11 @@ namespace sscs.cache
{ {
public class KeyValue public class KeyValue
{ {
public static int VALUE_TYPE_STRING = 0;
public static int VALUE_TYPE_BINARY = 1;
private int m_iValueType = VALUE_TYPE_STRING;
private string m_key; private string m_key;
public string Key public string Key
{ {
@ -27,6 +32,12 @@ namespace sscs.cache
return (DecryptValue()); return (DecryptValue());
} }
public byte[] GetValueAsBytes()
{
return (DecyptValueAsBytes());
}
// this sets string values
public void SetValue(string sValue) public void SetValue(string sValue)
{ {
@ -42,6 +53,14 @@ namespace sscs.cache
} }
} }
// this sets binary values
public void SetValue(byte[] baValue)
{
m_iValueType = VALUE_TYPE_BINARY;
m_value = EncryptValue(baValue);
m_modified = DateTime.Now;
}
/* /*
public string Value public string Value
{ {
@ -119,18 +138,37 @@ namespace sscs.cache
m_value = EncryptValue(sValue); m_value = EncryptValue(sValue);
} }
public KeyValue(string sKey, byte[] baValue)
{
// NOTE: Order is important, do not change
m_created = m_modified = DateTime.Now;
m_key = sKey;
m_value = EncryptValue(baValue);
m_iValueType = VALUE_TYPE_BINARY;
}
private byte[] EncryptValue(string sValue) private byte[] EncryptValue(string sValue)
{ {
byte[] baValueClear = Encoding.Default.GetBytes(sValue); byte[] baValueClear = Encoding.Default.GetBytes(sValue);
return (XORValue(baValueClear)); return EncryptValue(baValueClear);
} }
private byte[] EncryptValue(byte[] baValueClear)
{
return (XORValue(baValueClear));
}
private string DecryptValue() private string DecryptValue()
{ {
byte[] baValueClear = XORValue(m_value); byte[] baValueClear = DecyptValueAsBytes();
return Encoding.Default.GetString(baValueClear); return Encoding.Default.GetString(baValueClear);
} }
private byte[] DecyptValueAsBytes()
{
return XORValue(m_value);
}
private byte[] XORValue(byte[] baInput) private byte[] XORValue(byte[] baInput)
{ {
@ -145,6 +183,11 @@ namespace sscs.cache
return baOutput; return baOutput;
} }
public int GetValueType()
{
return m_iValueType;
}
public string ToXML() public string ToXML()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -146,7 +146,10 @@ namespace sscs.cache
sb.Append(kv.Key); sb.Append(kv.Key);
sb.Append("="); sb.Append("=");
sb.Append(kv.GetValue()); if (kv.GetValueType() ==(KeyValue.VALUE_TYPE_BINARY))
sb.Append("BINARY - Do not change");
else
sb.Append(kv.GetValue());
sb.Append('\n'); sb.Append('\n');
} }
//sb.Append('\0'); //sb.Append('\0');
@ -169,6 +172,7 @@ namespace sscs.cache
{ {
return secretID; return secretID;
} }
public void SetKeyValue(string key, string value) public void SetKeyValue(string key, string value)
{ {
KeyValue kv; KeyValue kv;
@ -185,6 +189,22 @@ namespace sscs.cache
this.ModifiedTime = DateTime.Now; this.ModifiedTime = DateTime.Now;
} }
public void SetKeyValue(string key, byte[] baValue)
{
KeyValue kv;
if (htKeyValues.Contains(key))
{
kv = (KeyValue)htKeyValues[key];
kv.SetValue(baValue);
}
else
{
kv = new KeyValue(key, baValue);
htKeyValues.Add(key, kv);
}
this.ModifiedTime = DateTime.Now;
}
public KeyValue GetKeyValue(string key) public KeyValue GetKeyValue(string key)
{ {
this.AccessedTime = DateTime.Now; this.AccessedTime = DateTime.Now;

View File

@ -59,6 +59,8 @@ namespace sscs.common
msgIdMap.Add(18,"sscs.verbs.SetMasterPassword"); msgIdMap.Add(18,"sscs.verbs.SetMasterPassword");
msgIdMap.Add(19,"sscs.verbs.IsSecretPersistent"); msgIdMap.Add(19,"sscs.verbs.IsSecretPersistent");
msgIdMap.Add(20,"sscs.verbs.ObjectSerialization"); msgIdMap.Add(20,"sscs.verbs.ObjectSerialization");
msgIdMap.Add(21,"sscs.verbs.WriteBinaryKey");
msgIdMap.Add(22,"sscs.verbs.ReadBinaryKey");
} }

View File

@ -37,9 +37,11 @@ OBJS=\
verbs/RemoveSecret \ verbs/RemoveSecret \
verbs/EnumerateKeyChainIds \ verbs/EnumerateKeyChainIds \
verbs/EnumerateSecretIds \ verbs/EnumerateSecretIds \
verbs/SetMasterPassword \ verbs/SetMasterPassword \
verbs/WriteKey \ verbs/WriteKey \
verbs/ReadKey \ verbs/ReadKey \
verbs/WriteBinaryKey \
verbs/ReadBinaryKey \
verbs/IsSecretPersistent \ verbs/IsSecretPersistent \
verbs/ObjectSerialization \ verbs/ObjectSerialization \
test/cache/TestSecret \ test/cache/TestSecret \

View File

@ -40,6 +40,8 @@ SRC=\
verbs/SetMasterPassword.cs \ verbs/SetMasterPassword.cs \
verbs/WriteKey.cs \ verbs/WriteKey.cs \
verbs/ReadKey.cs \ verbs/ReadKey.cs \
verbs/WriteBinaryKey.cs \
verbs/ReadBinaryKey.cs \
verbs/IsSecretPersistent.cs \ verbs/IsSecretPersistent.cs \
verbs/ObjectSerialization.cs \ verbs/ObjectSerialization.cs \
test/cache/TestSecret.cs \ test/cache/TestSecret.cs \