#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
{
///
/// Parameters for the Skein hash function - a series of byte[] strings identified by integer tags.
///
///
/// Parameterised Skein can be used for:
///
/// - MAC generation, by providing a key.
/// - Randomised hashing, by providing a nonce.
/// - A hash function for digital signatures, associating a
/// public key with the message digest.
/// - A key derivation function, by providing a
/// key identifier.
/// - Personalised hashing, by providing a
/// recommended format or
/// arbitrary personalisation string.
///
///
///
///
///
public class SkeinParameters
: ICipherParameters
{
///
/// The parameter type for a secret key, supporting MAC or KDF functions: 0
///
public const int PARAM_TYPE_KEY = 0;
///
/// The parameter type for the Skein configuration block: 4
///
public const int PARAM_TYPE_CONFIG = 4;
///
/// The parameter type for a personalisation string: 8
///
public const int PARAM_TYPE_PERSONALISATION = 8;
///
/// The parameter type for a public key: 12
///
public const int PARAM_TYPE_PUBLIC_KEY = 12;
///
/// The parameter type for a key identifier string: 16
///
public const int PARAM_TYPE_KEY_IDENTIFIER = 16;
///
/// The parameter type for a nonce: 20
///
public const int PARAM_TYPE_NONCE = 20;
///
/// The parameter type for the message: 48
///
public const int PARAM_TYPE_MESSAGE = 48;
///
/// The parameter type for the output transformation: 63
///
public const int PARAM_TYPE_OUTPUT = 63;
private IDictionary parameters;
public SkeinParameters()
: this(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable())
{
}
private SkeinParameters(IDictionary parameters)
{
this.parameters = parameters;
}
///
/// Obtains a map of type (int) to value (byte[]) for the parameters tracked in this object.
///
public IDictionary GetParameters()
{
return parameters;
}
///
/// Obtains the value of the key parameter, or null
if not
/// set.
///
/// The key.
public byte[] GetKey()
{
return (byte[])parameters[PARAM_TYPE_KEY];
}
///
/// Obtains the value of the personalisation parameter, or
/// null
if not set.
///
public byte[] GetPersonalisation()
{
return (byte[])parameters[PARAM_TYPE_PERSONALISATION];
}
///
/// Obtains the value of the public key parameter, or
/// null
if not set.
///
public byte[] GetPublicKey()
{
return (byte[])parameters[PARAM_TYPE_PUBLIC_KEY];
}
///
/// Obtains the value of the key identifier parameter, or
/// null
if not set.
///
public byte[] GetKeyIdentifier()
{
return (byte[])parameters[PARAM_TYPE_KEY_IDENTIFIER];
}
///
/// Obtains the value of the nonce parameter, or null
if
/// not set.
///
public byte[] GetNonce()
{
return (byte[])parameters[PARAM_TYPE_NONCE];
}
///
/// A builder for .
///
public class Builder
{
private IDictionary parameters = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
public Builder()
{
}
public Builder(IDictionary paramsMap)
{
IEnumerator keys = paramsMap.Keys.GetEnumerator();
while (keys.MoveNext())
{
int key = (int)keys.Current;
parameters.Add(key, paramsMap[key]);
}
}
public Builder(SkeinParameters parameters)
{
IEnumerator keys = parameters.parameters.Keys.GetEnumerator();
while (keys.MoveNext())
{
int key = (int)keys.Current;
this.parameters.Add(key, parameters.parameters[key]);
}
}
///
/// Sets a parameters to apply to the Skein hash function.
///
///
/// Parameter types must be in the range 0,5..62, and cannot use the value 48
/// (reserved for message body).
///
/// Parameters with type < 48 are processed before
/// the message content, parameters with type > 48
/// are processed after the message and prior to output.
///
/// the type of the parameter, in the range 5..62.
/// the byte sequence of the parameter.
public Builder Set(int type, byte[] value)
{
if (value == null)
{
throw new ArgumentException("Parameter value must not be null.");
}
if ((type != PARAM_TYPE_KEY)
&& (type <= PARAM_TYPE_CONFIG || type >= PARAM_TYPE_OUTPUT || type == PARAM_TYPE_MESSAGE))
{
throw new ArgumentException("Parameter types must be in the range 0,5..47,49..62.");
}
if (type == PARAM_TYPE_CONFIG)
{
throw new ArgumentException("Parameter type " + PARAM_TYPE_CONFIG
+ " is reserved for internal use.");
}
this.parameters.Add(type, value);
return this;
}
///
/// Sets the parameter.
///
public Builder SetKey(byte[] key)
{
return Set(PARAM_TYPE_KEY, key);
}
///
/// Sets the parameter.
///
public Builder SetPersonalisation(byte[] personalisation)
{
return Set(PARAM_TYPE_PERSONALISATION, personalisation);
}
///
/// Implements the recommended personalisation format for Skein defined in Section 4.11 of
/// the Skein 1.3 specification.
///
///
/// The format is YYYYMMDD email@address distinguisher
, encoded to a byte
/// sequence using UTF-8 encoding.
///
/// the date the personalised application of the Skein was defined.
/// the email address of the creation of the personalised application.
/// an arbitrary personalisation string distinguishing the application.
public Builder SetPersonalisation(DateTime date, string emailAddress, string distinguisher)
{
try
{
MemoryStream bout = new MemoryStream();
StreamWriter outBytes = new StreamWriter(bout, System.Text.Encoding.UTF8);
outBytes.Write(date.ToString("YYYYMMDD", CultureInfo.InvariantCulture));
outBytes.Write(" ");
outBytes.Write(emailAddress);
outBytes.Write(" ");
outBytes.Write(distinguisher);
BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(outBytes);
return Set(PARAM_TYPE_PERSONALISATION, bout.ToArray());
}
catch (IOException e)
{
throw new InvalidOperationException("Byte I/O failed.", e);
}
}
///
/// Sets the parameter.
///
public Builder SetPublicKey(byte[] publicKey)
{
return Set(PARAM_TYPE_PUBLIC_KEY, publicKey);
}
///
/// Sets the parameter.
///
public Builder SetKeyIdentifier(byte[] keyIdentifier)
{
return Set(PARAM_TYPE_KEY_IDENTIFIER, keyIdentifier);
}
///
/// Sets the parameter.
///
public Builder SetNonce(byte[] nonce)
{
return Set(PARAM_TYPE_NONCE, nonce);
}
///
/// Constructs a new instance with the parameters provided to this
/// builder.
///
public SkeinParameters Build()
{
return new SkeinParameters(parameters);
}
}
}
}
#pragma warning restore
#endif