The following gists implement PBKDF2 in C# for Universal Windows Platform (UWP). The class should be portable to Windows 10, Windows 10 Mobile, and WinRT.
The class will return a hex string using the default parameters. By default the string will be sized to 16 bytes, and will be hashed 100000 times
string password = "my secret password";
string salt = "<salt>"; // Should be sized to 64 bytes for SHA256, and 128 for SHA512
string pbdfk = new PBKDF(password, salt).hash;
System.Diagnostics.Debug.WriteLine(pbkdf); // Output in HEX format
Optionally, the algorithm
paramter can be passed to change the default algorithm from Pbkdf2Sha256
. The algorithm string may be one of the following
KeyDerivationAlgorithmNames.Pbkdf2Md5 // PBKDF2_MD5
KeyDerivationAlgorithmNames.Pbkdf2Sha1 // PBKDF2_SHA1
KeyDerivationAlgorithmNames.Pbkdf2Sha256 // PBKDF2_SHA256
KeyDerivationAlgorithmNames.Pbkdf2Sha384 // PBKDF2_SHA384
KeyDerivationAlgorithmNames.Pbkdf2Sha512 // PBKDF2_SHA512
Example as shown:
string pbdfk = new PBKDF(password, salt, KeyDerivationAlgorithmNames.Pbkdf2Sha512).hash;
Additionally, the iteration count and target size can be adjusted by providing the next two paramters
// SHA-256 PBKDK2, 150000 iterations, with a 32 bit output size
string pbdfk = new PBKDF(password, salt, KeyDerivationAlgorithmNames.Pbkdf2Sha256, 150000, 32).hash;
As previously mentioned, the result will be returned as a hex string, but can be converted back to an IBuffer
using the CryptographicBuffer
class.
string pbdfk = new PBKDF(password, salt).hash;
IBuffer keyMaterials = CryptographicBuffer.DecodeFromHexString(hash);