//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Messaging.Bindings {
using System;
using System.Collections.Generic;
using Validation;
///
/// A trivial implementation of that has only one fixed key.
/// This is meant for simple, low-security applications. Greater security requires an
/// implementation of that actually stores and retrieves
/// keys from a persistent store.
///
public class HardCodedKeyCryptoKeyStore : ICryptoKeyStore {
///
/// The handle to report for the hard-coded key.
///
private const string HardCodedKeyHandle = "fxd";
///
/// The one crypto key singleton instance.
///
private readonly CryptoKey OneCryptoKey;
///
/// Initializes a new instance of the class.
///
/// The 256-bit secret as a base64 encoded string.
public HardCodedKeyCryptoKeyStore(string secretAsBase64)
: this(Convert.FromBase64String(Requires.NotNull(secretAsBase64, "secretAsBase64"))) {
}
///
/// Initializes a new instance of the class.
///
/// The 256-bit secret.
public HardCodedKeyCryptoKeyStore(byte[] secret) {
Requires.NotNull(secret, "secret");
this.OneCryptoKey = new CryptoKey(secret, DateTime.MaxValue.AddDays(-2).ToUniversalTime());
}
#region ICryptoKeyStore Members
///
/// Gets the key in a given bucket and handle.
///
/// The bucket name. Case sensitive.
/// The key handle. Case sensitive.
///
/// The cryptographic key, or null if no matching key was found.
///
public CryptoKey GetKey(string bucket, string handle) {
if (handle == HardCodedKeyHandle) {
return this.OneCryptoKey;
}
return null;
}
///
/// Gets a sequence of existing keys within a given bucket.
///
/// The bucket name. Case sensitive.
///
/// A sequence of handles and keys, ordered by descending .
///
public IEnumerable> GetKeys(string bucket) {
return new[] { new KeyValuePair(HardCodedKeyHandle, this.OneCryptoKey) };
}
///
/// Stores a cryptographic key.
///
/// The name of the bucket to store the key in. Case sensitive.
/// The handle to the key, unique within the bucket. Case sensitive.
/// The key to store.
/// Always thrown.
public void StoreKey(string bucket, string handle, CryptoKey key) {
throw new NotSupportedException();
}
///
/// Removes the key.
///
/// The bucket name. Case sensitive.
/// The key handle. Case sensitive.
/// Always thrown.
public void RemoveKey(string bucket, string handle) {
throw new NotSupportedException();
}
#endregion
}
}