Skip to content

Commit 8618eb3

Browse files
anikammsftmihaelablendea
authored andcommitted
Add pbkdf2 password hashing tool (#716)
* Add pbkdf2 password hashing tool * Binaries
1 parent 592c490 commit 8618eb3

7 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
obj/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.AspNet.Cryptography.KeyDerivation;
2+
using System;
3+
using System.Linq;
4+
5+
namespace pbkdf2
6+
{
7+
class Program
8+
{
9+
/// <summary>
10+
/// Main method
11+
/// </summary>
12+
static void Main(string[] args)
13+
{
14+
if (args.Length < 2)
15+
{
16+
PrintUsage();
17+
System.Environment.Exit(1);
18+
}
19+
20+
string password = args[0];
21+
string hexSalt = args[1];
22+
23+
byte[] salt = StringToByteArray(hexSalt);
24+
Console.WriteLine(GetHashedPassword(password, salt));
25+
}
26+
27+
/// <summary>
28+
/// Convert hex string to byte array
29+
/// </summary>
30+
/// <param name="hex">Hexadecimal string</param>
31+
/// <returns>Byte array representation of the string</returns>
32+
private static byte[] StringToByteArray(string hex) => Enumerable.Range(0, hex.Length)
33+
.Where(x => x % 2 == 0)
34+
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
35+
.ToArray();
36+
37+
/// <summary>
38+
/// Generate hashed password with the given salt
39+
/// </summary>
40+
/// <param name="password">Password to be hashed</param>
41+
/// <param name="salt">Byte array of salt</param>
42+
/// <returns>Hashed and salted password</returns>
43+
private static string GetHashedPassword(String password, byte[] salt)
44+
{
45+
// Derive a 32 bytes subkey (use HMACSHA1 with 10,000 iterations)
46+
//
47+
return Convert.ToBase64String(KeyDerivation.Pbkdf2(
48+
password: password,
49+
salt: salt,
50+
prf: KeyDerivationPrf.HMACSHA1,
51+
iterationCount: 10000,
52+
numBytesRequested: 32));
53+
}
54+
55+
/// <summary>
56+
/// Prints usage of the program
57+
/// </summary>
58+
private static void PrintUsage()
59+
{
60+
Console.WriteLine("Usage:");
61+
Console.WriteLine("pbkdf2 <password> <hex salt>");
62+
}
63+
}
64+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# PBKDF2
2+
3+
PBKDF2 (Password-Based Key Derivation Function 2) is a password hashing mechanism. This tool takes in a password and a hexadecimal string as a salt to generate a hash of the password.
4+
5+
## Building and using the pbkdf2 tool
6+
7+
### Pre-requisites
8+
.NET Core installation is required to build the project.
9+
.NET Core can be downloaded from [here](https://dotnet.microsoft.com/download).
10+
11+
### Build Instructions
12+
* For building a Windows 10 binary, run "dotnet publish -c Release -r win10-x64"
13+
* For building an Ubuntu Linux binary, run "dotnet publish -c Release -r ubuntu.16.04-x64"
14+
* For building an OSX binary, run "dotnet publish -c Release -r osx-x64"
15+
16+
Upon running the above command(s), a self-contained package will be built under bin/Release folder. You may directly run the binary from there.
17+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RuntimeIdentifiers>win10-x64;ubuntu.16.04-x64;osx-x64</RuntimeIdentifiers>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNet.Cryptography.Internal" Version="1.0.0-rc1-final" />
11+
<PackageReference Include="Microsoft.AspNet.Cryptography.KeyDerivation" Version="1.0.0-rc1-final" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)