Last active
December 10, 2020 12:38
-
-
Save junxy/6657017 to your computer and use it in GitHub Desktop.
Sample C# code to create SHA1 Salted (SSHA) password hashes for OpenLDAP
Via:http://blogs.msdn.com/b/alextch/archive/2012/05/12/sample-c-code-to-create-sha1-salted-ssha-password-hashes-for-openldap.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GenerateSaltedSHA1(string plainTextString) | |
{ | |
HashAlgorithm algorithm = new SHA1Managed(); | |
var saltBytes = GenerateSalt(4); | |
var plainTextBytes = Encoding.ASCII.GetBytes(plainTextString); | |
var plainTextWithSaltBytes = AppendByteArray(plainTextBytes, saltBytes); | |
var saltedSHA1Bytes = algorithm.ComputeHash(plainTextWithSaltBytes); | |
var saltedSHA1WithAppendedSaltBytes = AppendByteArray(saltedSHA1Bytes, saltBytes); | |
return "{SSHA}" + Convert.ToBase64String(saltedSHA1WithAppendedSaltBytes); | |
} | |
private static byte[] GenerateSalt(int saltSize) | |
{ | |
var rng = new RNGCryptoServiceProvider(); | |
var buff = new byte[saltSize]; | |
rng.GetBytes(buff); | |
return buff; | |
} | |
private static byte[] AppendByteArray(byte[] byteArray1, byte[] byteArray2) | |
{ | |
var byteArrayResult = | |
new byte[byteArray1.Length + byteArray2.Length]; | |
for (var i = 0; i < byteArray1.Length; i++) | |
byteArrayResult[i] = byteArray1[i]; | |
for (var i = 0; i < byteArray2.Length; i++) | |
byteArrayResult[byteArray1.Length + i] = byteArray2[i]; | |
return byteArrayResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment