// WSSE Validation - 2007-04-30 - Robert Price - www.robertprice.co.uk
using System;
///
/// Computes WSSE codes from input for validation.
///
public static class WSSE
{
///
/// Calculate the digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// A string containing the digest.
public static string GetDigest(string password, string nonce, string created)
{
return GetDigest(password, nonce, created, System.Text.Encoding.ASCII);
}
///
/// Calculate the digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// The System.Text.Encoding to use.
/// A string containing the digest.
public static string GetDigest(string password, string nonce, string created, System.Text.Encoding enc)
{
byte[] noncebytes = enc.GetBytes(nonce);
byte[] passwordbytes = enc.GetBytes(password);
byte[] createdbytes = enc.GetBytes(created);
return generatecode(noncebytes, passwordbytes, createdbytes);
}
///
/// Calculate the alternative digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// A string containing the digest.
public static string GetDigestAlt(string password, string nonce, string created)
{
return GetDigestAlt(password, nonce, created, System.Text.Encoding.ASCII);
}
///
/// Calculate the alternative digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// The System.Text.Encoding to use.
/// A string containing the digest.
public static string GetDigestAlt(string password, string nonce, string created, System.Text.Encoding enc)
{
byte[] noncebytes = Convert.FromBase64String(nonce);
byte[] passwordbytes = enc.GetBytes(password);
byte[] createdbytes = enc.GetBytes(created);
return generatecode(noncebytes, passwordbytes, createdbytes);
}
private static string generatecode(byte[] nonce, byte[] password, byte[] created)
{
byte[] code = new byte[nonce.Length + password.Length + created.Length];
Array.Copy(nonce, code, nonce.Length);
Array.Copy(created, 0, code, nonce.Length, created.Length);
Array.Copy(password, 0, code, nonce.Length + created.Length, password.Length);
System.Security.Cryptography.SHA1Managed SHhash = new System.Security.Cryptography.SHA1Managed();
return Convert.ToBase64String(SHhash.ComputeHash(code));
}
///
/// Validates password, nonce and created create the same digest code as digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// The digest to validate the password, nonce and created strings against as a string.
/// true or false depending on wether the digest validates
public static bool IsValid(string password, string nonce, string created, string digest)
{
return digest == GetDigest(password, nonce, created) || digest == GetDigestAlt(password, nonce, created);
}
///
/// Validates password, nonce and created create the same digest code as digest.
///
/// A string with your unencrypted password.
/// The nonce the digest is to be created with as a string.
/// The timestamp as a string.
/// The digest to validate the password, nonce and created strings against as a string.
/// A System.Text.Encoding encoding.
/// true or false depending on wether the digest validates.
public static bool IsValid(string password, string nonce, string created, string digest, System.Text.Encoding enc)
{
return digest == GetDigest(password, nonce, created, enc) || digest == GetDigestAlt(password, nonce, created, enc);
}
}