Class CryptographyHelper
Provides helper methods to work with cryptographic service provider API.
Inheritance
Inherited Members
Namespace: Nevatech.Vsb.Repository.Security.Cryptography
Assembly: Nevatech.Vsb.Repository.dll
Syntax
public static class CryptographyHelper
Fields
Random
An initialized instance of the pseudo-random number generator.
Declaration
public static readonly Random Random
Field Value
Type | Description |
---|---|
System.Random |
Methods
BytesToString(Byte[])
Converts string to array of bytes using UTF-8 encoding.
Declaration
public static string BytesToString(byte[] value)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | value | Array of bytes to be converted. |
Returns
Type | Description |
---|---|
System.String | Bytes representing original string. |
BytesToString(Byte[], Int32, Int32, String, String)
Returns string representation of byte-array using specified format string that applied to each byte in array.
Declaration
public static string BytesToString(byte[] value, int index, int length, string formatString, string separator)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | value | Array of bytes to be printed. |
System.Int32 | index | Index of the first byte to print. |
System.Int32 | length | Max number of bytes to print, or 0 to print all bytes. |
System.String | formatString | Format string that will be applied to each byte in array. |
System.String | separator | String that will be added between subsequent bytes (may be Null or empty). |
Returns
Type | Description |
---|---|
System.String | String representation of byte-array. |
CompareArrays(Byte[], Byte[])
Compares values in two provided arrays of bytes.
Declaration
public static bool CompareArrays(byte[] arrayA, byte[] arrayB)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | arrayA | First array to compare with. |
System.Byte[] | arrayB | Second array to compare with. |
Returns
Type | Description |
---|---|
System.Boolean | True, if arrays are equal or Null. False, otherwise. |
ComputeHash(String)
Computes hash value of provided string. Method automatically generates entropy value and prepend it to the result. Use ValidateHash(String, Byte[]) overload to check hash value generated by this method.
Declaration
public static byte[] ComputeHash(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String that hash value needs to be calculated for. |
Returns
Type | Description |
---|---|
System.Byte[] | Calculated hash value with prepended salt value. |
Remarks
Method uses 4 bytes of entropy and SHA1 function for hash computation and results to 24 bytes value.
ComputeHash(String, Byte[])
Computes hash value of provided string with prepended salt value.
Declaration
public static byte[] ComputeHash(string value, byte[] salt)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String that hash value needs to be calculated for. |
System.Byte[] | salt | Random set of bytes (optional). |
Returns
Type | Description |
---|---|
System.Byte[] | Calculated hash value. |
Remarks
Method uses SHA1 function for hash computation which results to 20 bytes value.
ComputeHash(String, Int32)
Computes hash value of provided string with prepended salt value.
Declaration
public static byte[] ComputeHash(string value, int salt)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String that hash value needs to be calculated for. |
System.Int32 | salt | Random integer value. |
Returns
Type | Description |
---|---|
System.Byte[] | Calculated hash value. |
Remarks
Method uses SHA1 function for hash computation which results to 20 bytes value.
ComputeHash(String, String)
Computes hash value of the provided string using the specified hashing algorithm.
Declaration
public static string ComputeHash(string value, string hashName)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to be hashed. |
System.String | hashName | The hash algorithm implementation to use: SHA1, SHA256, SHA384, SHA512, or MD5. |
Returns
Type | Description |
---|---|
System.String | Base64-encoded hash value of the provided string. |
CreateSalt()
Creates random cryptographically strong integer value.
Declaration
public static int CreateSalt()
Returns
Type | Description |
---|---|
System.Int32 | Random cryptographically strong integer value. |
CreateSalt(Int32)
Creates array of bytes filled with cryptographically strong sequence of random values.
Declaration
public static byte[] CreateSalt(int size)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | size | Number of bytes to generate. |
Returns
Type | Description |
---|---|
System.Byte[] | Array of |
CreateSignature(Byte[], Byte[])
Computes and returns digital signature value using provided data and private key.
Declaration
public static byte[] CreateSignature(byte[] data, byte[] privateKey)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data to be signed. |
System.Byte[] | privateKey | Public and private key pair which will be used to sign data. |
Returns
Type | Description |
---|---|
System.Byte[] | Array of bytes that contains signature value. |
DecryptToBytes(Byte[])
Decrypts provided data using Windows Data Protection API.
Declaration
public static byte[] DecryptToBytes(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Encrypted data. |
Returns
Type | Description |
---|---|
System.Byte[] | Decrypted data. |
DecryptToBytes(String)
Decrypts provided base64 string using Windows Data Protection API.
Declaration
public static byte[] DecryptToBytes(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Base64 encoded encrypted data. |
Returns
Type | Description |
---|---|
System.Byte[] | Decrypted data. |
DecryptToString(Byte[])
Decrypts provided data using Windows Data Protection API and returns decrypted string.
Declaration
public static string DecryptToString(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Encrypted data. |
Returns
Type | Description |
---|---|
System.String | Decrypted string. |
DecryptToString(String)
Decrypts provided base64 string using Windows Data Protection API and returns decrypted string.
Declaration
public static string DecryptToString(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Base64 encoded encrypted data. |
Returns
Type | Description |
---|---|
System.String | Decrypted string. |
DecryptWithCertificate(String, X509Certificate2)
Decrypts the encrypted base64-encoded text with the RSA private key stored in the provided X.509 certificate.
Declaration
public static string DecryptWithCertificate(string encryptedText, X509Certificate2 certificate)
Parameters
Type | Name | Description |
---|---|---|
System.String | encryptedText | The base64-encoded encrypted text to decrypt. |
System.Security.Cryptography.X509Certificates.X509Certificate2 | certificate | X.509 certificate to be used for asymmetrical encryption. |
Returns
Type | Description |
---|---|
System.String | A decrypted plain text. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified |
DecryptWithPassword(Byte[], String)
Decrypts encrypted data with a password using AES algorithm.
Declaration
public static byte[] DecryptWithPassword(byte[] encryptedData, string password)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | encryptedData | The encrypted data to decrypt. |
System.String | password | The password to decrypt with. |
Returns
Type | Description |
---|---|
System.Byte[] | A byte array that contains decrypted data. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified encryptedData or password is null. |
DecryptWithPassword(String, String)
Decrypts the encrypted base64-encoded text with a password using AES algorithm.
Declaration
public static string DecryptWithPassword(string encryptedText, string password)
Parameters
Type | Name | Description |
---|---|---|
System.String | encryptedText | The base64-encoded encrypted text to decrypt. |
System.String | password | The password to decrypt with. |
Returns
Type | Description |
---|---|
System.String | A decrypted plain text. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified encryptedText or password is null. |
EncryptToBytes(Byte[])
Encrypts provided data using Windows Data Protection API.
Declaration
public static byte[] EncryptToBytes(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data to be encrypted. |
Returns
Type | Description |
---|---|
System.Byte[] | Encrypted data. |
EncryptToBytes(String)
Encrypts provided string using Windows Data Protection API.
Declaration
public static byte[] EncryptToBytes(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to be encrypted. |
Returns
Type | Description |
---|---|
System.Byte[] | Encrypted data. |
EncryptToString(Byte[])
Encrypts provided data using Windows Data Protection API and returns encrypted data as base64 string.
Declaration
public static string EncryptToString(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data to be encrypted. |
Returns
Type | Description |
---|---|
System.String | Encrypted data encoded with Base 64 digits. |
EncryptToString(String)
Encrypts provided string using Windows Data Protection API and returns encrypted data as base64 string.
Declaration
public static string EncryptToString(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to be encrypted. |
Returns
Type | Description |
---|---|
System.String | Encrypted data encoded with Base 64 digits. |
EncryptWithCertificate(String, X509Certificate2)
Encrypts the provided text with the RSA public key stored in the provided X.509 certificate.
Declaration
public static string EncryptWithCertificate(string plainText, X509Certificate2 certificate)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText | The text to encrypt. |
System.Security.Cryptography.X509Certificates.X509Certificate2 | certificate | X.509 certificate to be used for asymmetrical encryption. |
Returns
Type | Description |
---|---|
System.String | An encrypted, base64-encoded string. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified |
EncryptWithPassword(Byte[], String)
Encrypts data with a password using AES algorithm.
Declaration
public static byte[] EncryptWithPassword(byte[] data, string password)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | The data to encrypt. |
System.String | password | The password to encrypt with. |
Returns
Type | Description |
---|---|
System.Byte[] | A byte array that contains encrypted data. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified data or password is null or empty. |
EncryptWithPassword(String, String)
Encrypts plain text with a password using AES algorithm.
Declaration
public static string EncryptWithPassword(string plainText, string password)
Parameters
Type | Name | Description |
---|---|---|
System.String | plainText | The text to encrypt. |
System.String | password | The password to encrypt with. |
Returns
Type | Description |
---|---|
System.String | An encrypted base64-encoded string. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Thrown if the specified |
GenerateSecret(SecretType, Int32)
Randomly generates a secret of the specified length.
Declaration
public static string GenerateSecret(SecretType secretType, int length)
Parameters
Type | Name | Description |
---|---|---|
SecretType | secretType | Type of the generated secret. Binary secret is returned as a base64-encoded string. |
System.Int32 | length | For binary secret: the number of generated bytes. For password: the password length in chars. For token and captcha: the number of characters. |
Returns
Type | Description |
---|---|
System.String | Password or base64-encoded binary secret. |
StringToBytes(String)
Converts array of bytes to string using UTF-8 encoding.
Declaration
public static byte[] StringToBytes(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | Bytes to be decoded. |
Returns
Type | Description |
---|---|
System.Byte[] | String that was encoded. |
ToSecureString(String)
Converts provided string to System.Security.SecureString.
Declaration
public static SecureString ToSecureString(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to be converted. |
Returns
Type | Description |
---|---|
System.Security.SecureString | System.Security.SecureString initialized from provided value, or Null if provided value was Null. |
ValidateHash(String, Byte[])
Compares provided hash value with computed hash value for specified string.
Parameter hash
should also include prepended salt value that was
originally used for hash calculation. Use this method to validate hash value calculated
by ComputeHash(String) overload.
Declaration
public static bool ValidateHash(string value, byte[] hash)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to be validated. |
System.Byte[] | hash | Hash value with prepended salt value calculated for provided
|
Returns
Type | Description |
---|---|
System.Boolean | True, if provided hash and salt correspond to specified value. False, otherwise. |
ValidateHash(String, Byte[], Byte[])
Compares provided hash value with computed hash value for specified string with prepended salt value.
Declaration
public static bool ValidateHash(string value, byte[] hash, byte[] salt)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to be validated. |
System.Byte[] | hash | Calculated hash for provided |
System.Byte[] | salt | Random set of bytes that was used to calculate hash value. |
Returns
Type | Description |
---|---|
System.Boolean | True, if provided hash and salt correspond to specified value. False, otherwise. |
ValidateHash(String, Byte[], Int32)
Compares provided hash value with computed hash value for specified string with prepended salt value.
Declaration
public static bool ValidateHash(string value, byte[] hash, int salt)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String value to be validated. |
System.Byte[] | hash | Calculated hash for provided |
System.Int32 | salt | Random integer value. |
Returns
Type | Description |
---|---|
System.Boolean | True, if provided hash and salt correspond to specified value. False, otherwise. |
VerifySignature(Byte[], Byte[], Byte[])
Verifies that provided binary data correspond to provided signature.
Declaration
public static bool VerifySignature(byte[] data, byte[] signature, byte[] publicKey)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data that was signed. |
System.Byte[] | signature | Signature value to be validated. |
System.Byte[] | publicKey | Public key from the key pair that was used to sign the data. |
Returns
Type | Description |
---|---|
System.Boolean | True, if signature is valid, False, if signature is invalid. |