Class SecurityHelper

Namespace: CapyKit.Helpers
Assembly: CapyKit.dll

A class that contains methods for managing secure data processing and cryptography.

public class SecurityHelper

Inheritance

objectSecurityHelper

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Extension Methods

ObjectExtensions.UpdateProperties<SecurityHelper>(SecurityHelper, SecurityHelper), ObjectExtensions.UpdateProperties(object, object)

Methods

CompareHashedPassword<T>(Password, string, byte[], params object[])

Compares an unencrypted password with a stored, encrypted existingPassword. This method uses the specified password algorithm type T to retrieve the hashed version of the password and then compares it with the existingPassword.

public static bool CompareHashedPassword<T>(Password existingPassword, string password, byte[] salt, params object[] args)

Parameters

existingPassword Password

The existing, encrypted password.

password string

The unencrypted password to be compared.

salt byte[]

The salt value used in password hashing.

args object[]

Additional arguments required for constructing the password algorithm instance.

Returns

bool

true if hash comparison succeeds, false if it fails.

Type Parameters

T

The type of the password hashing algorithm.

CompareHashedPassword(Password, string, byte[], IPasswordAlgorithm, params object[])

Compares an unencrypted password with a stored, encrypted existingPassword. This method uses the specified algorithm to retrieve the hashed version of the password and then compares it with the existingPassword.

public static bool CompareHashedPassword(Password existingPassword, string password, byte[] salt, IPasswordAlgorithm algorithm, params object[] args)

Parameters

existingPassword Password

The existing, encrypted password.

password string

The unencrypted password to be compared.

salt byte[]

The salt value used in password hashing.

algorithm IPasswordAlgorithm

The password hashing algorithm.

args object[]

Additional arguments required for constructing the password algorithm instance.

Returns

bool

true if hash comparison succeeds, false if it fails.

CompareSessionID(string, string)

Compares two session identifiers.

public static bool CompareSessionID(string first, string second)

Parameters

first string

The first session identifier.

second string

The second session identifier.

Returns

bool

true if comparison succeeds, false if not.

GetPassword<T>(string, params object[])

Retrieves a CapyKit.Password object using the specified password and generates a random salt value. Then it uses that salt to call the overloaded CapyKit.Helpers.SecurityHelper.GetPassword``1(System.String,System.Byte[],System.Object[]) method with the given password and the generated salt as arguments.

public static Password GetPassword<T>(string password, params object[] args)

Parameters

password string

The plaintext password to be hashed.

args object[]

Optional constructor arguments for the CapyKit.IPasswordAlgorithm implementation instance.

Returns

Password

A new CapyKit.Password object with the given password and a randomly generated salt, as well as an instance of T created using any optional constructor arguments provided.

Type Parameters

T

The type of CapyKit.IPasswordAlgorithm implementation to use.

See Also

SecurityHelper.SALT_SIZE

GetPassword<T>(string, byte[], params object[])

Retrieves a CapyKit.Password object using the specified password, salt, and optional constructor arguments.

public static Password GetPassword<T>(string password, byte[] salt, params object[] args) where T : IPasswordAlgorithm

Parameters

password string

The plaintext password to be hashed.

salt byte[]

A random value used as an additional input to the one-way function that hashes data, a password or passphrase. This is used to make each output different for the same input thus adding security.

args object[]

Optional constructor arguments for the CapyKit.IPasswordAlgorithm implementation instance.

Returns

Password

A new CapyKit.Password object with the given password and salt, as well as an instance of T created using the provided constructor arguments.

Type Parameters

T

The type of CapyKit.IPasswordAlgorithm implementation to use.

Remarks

This method uses reflection to find a constructor for the specified password algorithm type (T). It emits an error event if a suitable constructor is not found or if there is an error invoking the constructor.

GetRandomPassword(int, params ValidCharacterCollection[])

Gets a cryptographically strong random password.

public static string GetRandomPassword(int length, params ValidCharacterCollection[] validCharacters)

Parameters

length int

The length of the password to generate.

validCharacters ValidCharacterCollection[]

An array of CapyKit.Helpers.ValidCharacterCollection enumeration values representing the desired character sets.

Returns

string

The password.

GetRandomString(int)

A convenience method to generate a random string of the specified length using all character sets.

public static string GetRandomString(int length)

Parameters

length int

The desired length of the generated random string.

Returns

string

See Also

ValidCharacterCollection, SecurityHelper.GetRandomString(int, params ValidCharacterCollection[])

GetRandomString(int, params ValidCharacterCollection[])

Gets a cryptographically strong random string using the character values found in VALID_CHARACTERS.

public static string GetRandomString(int length, params ValidCharacterCollection[] validChars)

Parameters

length int

The length of the string to create.

validChars ValidCharacterCollection[]

Returns

string

The random string.

GetSalt(int)

Generates a random byte array that can act as a salt.

public static byte[] GetSalt(int length = 32)

Parameters

length int

(Optional) The desired length of the generated byte array.

Returns

byte[]

An array of byte.

Remarks

A default length of CapyKit.Helpers.SecurityHelper.SALT_SIZE is provided as a sane default. Larger values can be used for increased entropy.

Pbkdf2(string, byte[])

Generates a new CapyKit.Password object using the PBKDF2 algorithm with the provided password and salt.

public static Password Pbkdf2(string password, byte[] salt)

Parameters

password string

The clear text password to be hashed.

salt byte[]

A random value used to add an additional layer of security to the generated hash.

Returns

Password

A new CapyKit.Password object containing the hashed password and salt.

Remarks

This method uses the PBKDF2 (Password-Based Key Derivation Function 2) algorithm to generate a new password hash. The algorithm iteratively applies a pseudorandom function to the password and salt, which increases the security of the resulting hash.

Pbkdf2(string)

Generates a new CapyKit.Password object using the PBKDF2 algorithm with the provided password. This overload of the method generates a random salt value for added security.

public static Password Pbkdf2(string password)

Parameters

password string

The clear text password to be hashed.

Returns

Password

A new CapyKit.Password object containing the hashed password and a randomly generated salt.

Remarks

This method uses the PBKDF2 (Password-Based Key Derivation Function 2) algorithm to generate a new password hash. The algorithm iteratively applies a pseudorandom function to the password and salt, which increases the security of the resulting hash. In this overload, a random salt value is generated using CapyKit.Helpers.SecurityHelper.GetRandomBytes(System.Int32) method.