2006-01-19 00:34:21 +01:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2006 Novell, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
|
|
* you may find current contact information at www.novell.com.
|
|
|
|
*
|
|
|
|
***********************************************************************/
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
using Novell.CASA.MiCasa.Common;
|
|
|
|
|
|
|
|
namespace sscs.cache
|
|
|
|
{
|
|
|
|
public class KeyValue
|
|
|
|
{
|
2005-12-09 17:16:58 +01:00
|
|
|
public static int VALUE_TYPE_STRING = 0;
|
|
|
|
public static int VALUE_TYPE_BINARY = 1;
|
|
|
|
|
|
|
|
private int m_iValueType = VALUE_TYPE_STRING;
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
private string m_key;
|
|
|
|
public string Key
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
m_key = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private byte[] m_value;
|
|
|
|
|
|
|
|
public string GetValue()
|
|
|
|
{
|
|
|
|
return (DecryptValue());
|
|
|
|
}
|
|
|
|
|
2005-12-09 17:16:58 +01:00
|
|
|
public byte[] GetValueAsBytes()
|
|
|
|
{
|
|
|
|
return (DecyptValueAsBytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
// this sets string values
|
2005-10-11 21:51:00 +02:00
|
|
|
public void SetValue(string sValue)
|
|
|
|
{
|
|
|
|
|
|
|
|
string oldValue = DecryptValue();
|
|
|
|
|
|
|
|
if (oldValue.Equals(sValue))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
byte[] newEncryptedValue = EncryptValue(sValue);
|
|
|
|
m_value = newEncryptedValue;
|
|
|
|
m_modified = DateTime.Now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-09 17:16:58 +01:00
|
|
|
// this sets binary values
|
|
|
|
public void SetValue(byte[] baValue)
|
|
|
|
{
|
|
|
|
m_iValueType = VALUE_TYPE_BINARY;
|
|
|
|
m_value = EncryptValue(baValue);
|
|
|
|
m_modified = DateTime.Now;
|
|
|
|
}
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
/*
|
|
|
|
public string Value
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// decrypt
|
|
|
|
return (DecryptValue());
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
byte[] newEncrypteValue = EncryptValue(value);
|
|
|
|
|
|
|
|
if ((newEncrypteValue != null) && (newEncrypteValue.Equals(m_value)))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_value = newEncrypteValue;
|
|
|
|
m_modified = DateTime.Now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
private Hashtable mLinkedKeys = null; // = new Hashtable();
|
|
|
|
|
|
|
|
public void AddLink(LinkedKeyInfo linkedInfo)
|
|
|
|
{
|
|
|
|
if (mLinkedKeys == null)
|
|
|
|
mLinkedKeys = new Hashtable();
|
|
|
|
|
|
|
|
if(!mLinkedKeys.ContainsKey(linkedInfo.GetLinkID()))
|
|
|
|
mLinkedKeys.Add(linkedInfo.GetLinkID(), linkedInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveLink(string sLinkID)
|
|
|
|
{
|
|
|
|
// remove link
|
|
|
|
if (mLinkedKeys != null)
|
|
|
|
{
|
|
|
|
mLinkedKeys.Remove(sLinkID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Hashtable GetLinkedKeys()
|
|
|
|
{
|
|
|
|
return mLinkedKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private DateTime m_created;
|
|
|
|
public DateTime CreatedTime
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return m_created;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DateTime m_modified;
|
|
|
|
public DateTime ModifiedTime
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return m_modified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public KeyValue(string sKey, string sValue)
|
|
|
|
{
|
|
|
|
// NOTE: Order is important, do not change
|
|
|
|
m_created = m_modified = DateTime.Now;
|
|
|
|
m_key = sKey;
|
|
|
|
m_value = EncryptValue(sValue);
|
|
|
|
}
|
|
|
|
|
2005-12-09 17:16:58 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
private byte[] EncryptValue(string sValue)
|
|
|
|
{
|
|
|
|
byte[] baValueClear = Encoding.Default.GetBytes(sValue);
|
2005-12-09 17:16:58 +01:00
|
|
|
return EncryptValue(baValueClear);
|
2005-10-11 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2005-12-09 17:16:58 +01:00
|
|
|
private byte[] EncryptValue(byte[] baValueClear)
|
|
|
|
{
|
|
|
|
return (XORValue(baValueClear));
|
|
|
|
}
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
private string DecryptValue()
|
|
|
|
{
|
2005-12-09 17:16:58 +01:00
|
|
|
byte[] baValueClear = DecyptValueAsBytes();
|
2005-10-11 21:51:00 +02:00
|
|
|
return Encoding.Default.GetString(baValueClear);
|
|
|
|
}
|
2005-12-09 17:16:58 +01:00
|
|
|
|
|
|
|
private byte[] DecyptValueAsBytes()
|
|
|
|
{
|
|
|
|
return XORValue(m_value);
|
|
|
|
}
|
2005-10-11 21:51:00 +02:00
|
|
|
|
|
|
|
private byte[] XORValue(byte[] baInput)
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] baOutput = new byte[baInput.Length];
|
|
|
|
Random rand = new Random((int)m_created.Ticks);
|
|
|
|
|
|
|
|
for (int i=0; i<baInput.Length; i++)
|
|
|
|
{
|
|
|
|
baOutput[i] = (byte)((int)baInput[i] ^ rand.Next());
|
|
|
|
}
|
|
|
|
|
|
|
|
return baOutput;
|
|
|
|
}
|
|
|
|
|
2005-12-09 17:16:58 +01:00
|
|
|
public int GetValueType()
|
|
|
|
{
|
|
|
|
return m_iValueType;
|
|
|
|
}
|
|
|
|
|
2005-10-11 21:51:00 +02:00
|
|
|
public string ToXML()
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("<ENTRY>");
|
|
|
|
|
|
|
|
sb.Append("<KEY>");
|
|
|
|
sb.Append(m_key);
|
|
|
|
sb.Append("</KEY>");
|
|
|
|
|
|
|
|
sb.Append("<VALUE>");
|
|
|
|
sb.Append(m_value);
|
|
|
|
sb.Append("</VALUE>");
|
|
|
|
|
|
|
|
sb.Append("<MODIFIED>");
|
|
|
|
sb.Append(m_modified.Ticks);
|
|
|
|
sb.Append("</MODIFIED>");
|
|
|
|
|
|
|
|
sb.Append("</ENTRY>");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|