SkeinParameters
Parameters for the Skein hash function - a series of byte[] strings identified by integer tags.
using Org.BouncyCastle.Utilities.Collections;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class SkeinParameters : ICipherParameters
{
public class Builder
{
private Dictionary<int, byte[]> m_parameters;
public Builder()
{
m_parameters = new Dictionary<int, byte[]>();
}
public Builder(IDictionary<int, byte[]> paramsMap)
{
m_parameters = new Dictionary<int, byte[]>(paramsMap);
}
public Builder(SkeinParameters parameters)
: this(parameters.m_parameters)
{
}
public Builder Set(int type, byte[] value)
{
if (value == null)
throw new ArgumentException("Parameter value must not be null.");
if (type != 0 && (type <= 4 || type >= 63 || type == 48))
throw new ArgumentException("Parameter types must be in the range 0,5..47,49..62.");
if (type == 4)
throw new ArgumentException("Parameter type " + 4.ToString() + " is reserved for internal use.");
m_parameters.Add(type, value);
return this;
}
public Builder SetKey(byte[] key)
{
return Set(0, key);
}
public Builder SetPersonalisation(byte[] personalisation)
{
return Set(8, personalisation);
}
public Builder SetPersonalisation(DateTime date, string emailAddress, string distinguisher)
{
try {
MemoryStream memoryStream = new MemoryStream();
using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.UTF8)) {
streamWriter.Write(date.ToString("YYYYMMDD", CultureInfo.InvariantCulture));
streamWriter.Write(" ");
streamWriter.Write(emailAddress);
streamWriter.Write(" ");
streamWriter.Write(distinguisher);
}
return Set(8, memoryStream.ToArray());
} catch (IOException innerException) {
throw new InvalidOperationException("Byte I/O failed.", innerException);
}
}
public Builder SetPublicKey(byte[] publicKey)
{
return Set(12, publicKey);
}
public Builder SetKeyIdentifier(byte[] keyIdentifier)
{
return Set(16, keyIdentifier);
}
public Builder SetNonce(byte[] nonce)
{
return Set(20, nonce);
}
public SkeinParameters Build()
{
return new SkeinParameters(m_parameters);
}
}
public const int PARAM_TYPE_KEY = 0;
public const int PARAM_TYPE_CONFIG = 4;
public const int PARAM_TYPE_PERSONALISATION = 8;
public const int PARAM_TYPE_PUBLIC_KEY = 12;
public const int PARAM_TYPE_KEY_IDENTIFIER = 16;
public const int PARAM_TYPE_NONCE = 20;
public const int PARAM_TYPE_MESSAGE = 48;
public const int PARAM_TYPE_OUTPUT = 63;
private IDictionary<int, byte[]> m_parameters;
public SkeinParameters()
: this(new Dictionary<int, byte[]>())
{
}
private SkeinParameters(IDictionary<int, byte[]> parameters)
{
m_parameters = parameters;
}
public IDictionary<int, byte[]> GetParameters()
{
return m_parameters;
}
public byte[] GetKey()
{
return CollectionUtilities.GetValueOrNull(m_parameters, 0);
}
public byte[] GetPersonalisation()
{
return CollectionUtilities.GetValueOrNull(m_parameters, 8);
}
public byte[] GetPublicKey()
{
return CollectionUtilities.GetValueOrNull(m_parameters, 12);
}
public byte[] GetKeyIdentifier()
{
return CollectionUtilities.GetValueOrNull(m_parameters, 16);
}
public byte[] GetNonce()
{
return CollectionUtilities.GetValueOrNull(m_parameters, 20);
}
}
}