Skip to content

Commit d49dc79

Browse files
authored
Merge pull request #6 from johnkellyoxford/dev
readded utils
2 parents 5a00d2e + 3d7993c commit d49dc79

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

utils/BatchHasher.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
4+
namespace PBKDF2Hasher
5+
{
6+
public class BatchHasher
7+
{
8+
9+
public static byte[] generateSalt()
10+
{
11+
var csprng = new RNGCryptoServiceProvider();
12+
var salt = new byte[24];
13+
csprng.GetBytes(salt);
14+
return salt;
15+
}
16+
17+
public static int Main(string[] args)
18+
{
19+
if (args.Length == 0)
20+
{
21+
Console.WriteLine(@"Please give a word followed by a salt, seperated by a comma (with no spaces) to be hashed
22+
E.g beep,gxsnfYvKQIExdD233H8rpkx8MKp8KhdD - not beep, gxsnfYvKQIExdD233H8rpkx8MKp8KhdD");
23+
return 1;
24+
}
25+
byte[] salt = new byte[24];
26+
var SplitArgs = new string[args.Length][];
27+
for (int i = 0; i < args.Length; i++)
28+
{
29+
SplitArgs[i] = args[i].Split(',');
30+
if(SplitArgs[i].Length == 1)
31+
{
32+
Console.WriteLine("No salt detected - salt will be generated");
33+
salt = generateSalt();
34+
}
35+
}
36+
37+
var hashes = new byte[args.Length][];
38+
for (int i = 0; i < args.Length; i++)
39+
{
40+
byte[] hashValue;
41+
42+
43+
//Generate hash using salt
44+
using (var pbkdf2 = new Rfc2898DeriveBytes(SplitArgs[i][0], Convert.FromBase64String(SplitArgs[i][1]), 100000))
45+
{
46+
hashValue = pbkdf2.GetBytes(24);
47+
}
48+
hashes[i] = hashValue;
49+
}
50+
Console.WriteLine("Hashes come first, followed by a hiphen ('-') then the salt: ");
51+
for (int i = 0; i < args.Length; i++)
52+
{
53+
Console.WriteLine(Convert.ToBase64String(hashes[i]) + " - " + SplitArgs[i][1]);
54+
}
55+
56+
return 0;
57+
}
58+
}
59+
}
5 KB
Binary file not shown.

utils/Executables/Hasher.exe

5 KB
Binary file not shown.

utils/Executables/HelloWorld.exe

3.5 KB
Binary file not shown.

utils/Executables/Protector.exe

5.5 KB
Binary file not shown.

utils/Hasher.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
4+
namespace PBKDF2Hasher
5+
{
6+
public class Hasher
7+
{
8+
9+
public static byte[] GenerateSalt()
10+
{
11+
var csprng = new RNGCryptoServiceProvider();
12+
var salt = new byte[32];
13+
csprng.GetBytes(salt);
14+
Console.WriteLine("Salt generated - {0}", Convert.ToBase64String(salt));
15+
return salt;
16+
}
17+
18+
public static int GetIterations()
19+
{
20+
Console.WriteLine("Please enter a number of iterations. If no input is given the program will default to 1 million: ");
21+
22+
int iterations;
23+
24+
string input = Console.ReadLine();
25+
if (input == "")
26+
{
27+
Console.WriteLine("Using default iterations");
28+
return 1000000;
29+
}
30+
try
31+
{
32+
iterations = Int32.Parse(input);
33+
}
34+
catch (FormatException)
35+
{
36+
Console.WriteLine("Please enter a valid number");
37+
return GetIterations();
38+
}
39+
return iterations;
40+
}
41+
42+
public static int Main(string[] args)
43+
{
44+
if (args.Length > 3 || args.Length == 0)
45+
{
46+
Console.WriteLine("Please give a word to be hashed, and optionally a salt and an iteration number");
47+
return 1;
48+
}
49+
50+
Byte[] salt;
51+
int iterations;
52+
53+
if (args.Length > 1)
54+
{
55+
salt = Convert.FromBase64String(args[1]);
56+
}
57+
else
58+
{
59+
salt = GenerateSalt();
60+
}
61+
62+
if (args.Length == 3)
63+
{
64+
iterations = Convert.ToInt32(args[3]);
65+
}
66+
else
67+
{
68+
iterations = GetIterations();
69+
70+
}
71+
72+
byte[] hashValue;
73+
74+
//Generate hash using salt
75+
using (var pbkdf2 = new Rfc2898DeriveBytes(args[0], salt, iterations))
76+
{
77+
Console.WriteLine("Hashing...");
78+
hashValue = pbkdf2.GetBytes(64);
79+
}
80+
81+
Console.WriteLine("\nHash: " + Convert.ToBase64String(hashValue) + "\n" + "Salt: " + Convert.ToBase64String(salt));
82+
83+
84+
return 0;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)