Salted hash generator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












I've had a go at creating a small class in C# that can generated salted hashes from text. I would like to know how this could be improved (in terms of code style) and whether or not this code is secure enough that it could be used in a professional environment (it won't be).



Hasher.cs



using System.Security.Cryptography;

namespace HashAndSaltTest

public static class Hasher

private static readonly int MaxSaltLength = 32;

public static byte GenerateSaltedHash(byte plainText)

HashAlgorithm algorithm = new SHA256Managed();
byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

for (int i = 0; i < plainText.Length; i++)
saltedText[i] = plainText[i];

for (int i = 0; i < salt.Length; i++)
saltedText[plainText.Length + i] = salt[i];

return algorithm.ComputeHash(saltedText);


private static byte GenerateSalt()

byte salt = new byte[MaxSaltLength];

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())
random.GetNonZeroBytes(salt);

return salt;









share|improve this question





















  • What exactly do you want to use this code for? How do you intend to verify the hashed value?
    – Roland Illig
    May 13 at 16:03










  • If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
    – Alejandro
    May 14 at 1:02
















up vote
5
down vote

favorite












I've had a go at creating a small class in C# that can generated salted hashes from text. I would like to know how this could be improved (in terms of code style) and whether or not this code is secure enough that it could be used in a professional environment (it won't be).



Hasher.cs



using System.Security.Cryptography;

namespace HashAndSaltTest

public static class Hasher

private static readonly int MaxSaltLength = 32;

public static byte GenerateSaltedHash(byte plainText)

HashAlgorithm algorithm = new SHA256Managed();
byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

for (int i = 0; i < plainText.Length; i++)
saltedText[i] = plainText[i];

for (int i = 0; i < salt.Length; i++)
saltedText[plainText.Length + i] = salt[i];

return algorithm.ComputeHash(saltedText);


private static byte GenerateSalt()

byte salt = new byte[MaxSaltLength];

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())
random.GetNonZeroBytes(salt);

return salt;









share|improve this question





















  • What exactly do you want to use this code for? How do you intend to verify the hashed value?
    – Roland Illig
    May 13 at 16:03










  • If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
    – Alejandro
    May 14 at 1:02












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I've had a go at creating a small class in C# that can generated salted hashes from text. I would like to know how this could be improved (in terms of code style) and whether or not this code is secure enough that it could be used in a professional environment (it won't be).



Hasher.cs



using System.Security.Cryptography;

namespace HashAndSaltTest

public static class Hasher

private static readonly int MaxSaltLength = 32;

public static byte GenerateSaltedHash(byte plainText)

HashAlgorithm algorithm = new SHA256Managed();
byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

for (int i = 0; i < plainText.Length; i++)
saltedText[i] = plainText[i];

for (int i = 0; i < salt.Length; i++)
saltedText[plainText.Length + i] = salt[i];

return algorithm.ComputeHash(saltedText);


private static byte GenerateSalt()

byte salt = new byte[MaxSaltLength];

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())
random.GetNonZeroBytes(salt);

return salt;









share|improve this question













I've had a go at creating a small class in C# that can generated salted hashes from text. I would like to know how this could be improved (in terms of code style) and whether or not this code is secure enough that it could be used in a professional environment (it won't be).



Hasher.cs



using System.Security.Cryptography;

namespace HashAndSaltTest

public static class Hasher

private static readonly int MaxSaltLength = 32;

public static byte GenerateSaltedHash(byte plainText)

HashAlgorithm algorithm = new SHA256Managed();
byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

for (int i = 0; i < plainText.Length; i++)
saltedText[i] = plainText[i];

for (int i = 0; i < salt.Length; i++)
saltedText[plainText.Length + i] = salt[i];

return algorithm.ComputeHash(saltedText);


private static byte GenerateSalt()

byte salt = new byte[MaxSaltLength];

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())
random.GetNonZeroBytes(salt);

return salt;











share|improve this question












share|improve this question




share|improve this question








edited May 20 at 20:55









t3chb0t

31.9k54195




31.9k54195









asked May 12 at 22:11









Ioan Thomas

1546




1546











  • What exactly do you want to use this code for? How do you intend to verify the hashed value?
    – Roland Illig
    May 13 at 16:03










  • If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
    – Alejandro
    May 14 at 1:02
















  • What exactly do you want to use this code for? How do you intend to verify the hashed value?
    – Roland Illig
    May 13 at 16:03










  • If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
    – Alejandro
    May 14 at 1:02















What exactly do you want to use this code for? How do you intend to verify the hashed value?
– Roland Illig
May 13 at 16:03




What exactly do you want to use this code for? How do you intend to verify the hashed value?
– Roland Illig
May 13 at 16:03












If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
– Alejandro
May 14 at 1:02




If "plainttext" is ever going to be a password, this is not secure. SHA256 is not designed to handle passwords (use PBKDF2 or bCrypt instead). If you're not using it with passwords, I don't get the purpose of the salt. A bit more context of what kind of input is expected and what'll you use the hashes for is relevant when speaking about security.
– Alejandro
May 14 at 1:02










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










  • Because HashAlgorithm implements IDisposable you should enclose the usage of it in an using block as well.


  • Omitting braces although they might be optional can lead to hidden and therfor hard to find bugs. I would like to encourage you to always use braces.


  • Instead of using a for loop to copy plainText to saltedText and salt to saltedText you may take advantage of the Array.CopyTo() method.


  • Public methods should validate passed parameters.


  • Because you don't return the salt you can only generate a hash but you can't verify the hash.


Applying the mentioned points can lead to



public static byte GenerateSaltedHash(byte plainText)

if (plainText == null) throw new ArgumentNullException("plainText");
if (plainText.Length == 0) throw new ArgumentException("Length may not be zero", "plainText");

using (HashAlgorithm algorithm = new SHA256Managed())

byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

plainText.CopyTo(saltedText, 0);
salt.CopyTo(saltedText, plainText.Length);

return algorithm.ComputeHash(saltedText);



private static byte GenerateSalt()

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())

byte salt = new byte[MaxSaltLength];
random.GetNonZeroBytes(salt);
return salt;







share|improve this answer























  • Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
    – Ioan Thomas
    May 13 at 20:31










  • IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
    – Heslacher
    May 13 at 20:35










  • @Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
    – Sinjai
    May 20 at 21:11











  • I meant overwriting the passed array.
    – Heslacher
    May 22 at 5:51










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194283%2fsalted-hash-generator%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










  • Because HashAlgorithm implements IDisposable you should enclose the usage of it in an using block as well.


  • Omitting braces although they might be optional can lead to hidden and therfor hard to find bugs. I would like to encourage you to always use braces.


  • Instead of using a for loop to copy plainText to saltedText and salt to saltedText you may take advantage of the Array.CopyTo() method.


  • Public methods should validate passed parameters.


  • Because you don't return the salt you can only generate a hash but you can't verify the hash.


Applying the mentioned points can lead to



public static byte GenerateSaltedHash(byte plainText)

if (plainText == null) throw new ArgumentNullException("plainText");
if (plainText.Length == 0) throw new ArgumentException("Length may not be zero", "plainText");

using (HashAlgorithm algorithm = new SHA256Managed())

byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

plainText.CopyTo(saltedText, 0);
salt.CopyTo(saltedText, plainText.Length);

return algorithm.ComputeHash(saltedText);



private static byte GenerateSalt()

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())

byte salt = new byte[MaxSaltLength];
random.GetNonZeroBytes(salt);
return salt;







share|improve this answer























  • Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
    – Ioan Thomas
    May 13 at 20:31










  • IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
    – Heslacher
    May 13 at 20:35










  • @Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
    – Sinjai
    May 20 at 21:11











  • I meant overwriting the passed array.
    – Heslacher
    May 22 at 5:51














up vote
4
down vote



accepted










  • Because HashAlgorithm implements IDisposable you should enclose the usage of it in an using block as well.


  • Omitting braces although they might be optional can lead to hidden and therfor hard to find bugs. I would like to encourage you to always use braces.


  • Instead of using a for loop to copy plainText to saltedText and salt to saltedText you may take advantage of the Array.CopyTo() method.


  • Public methods should validate passed parameters.


  • Because you don't return the salt you can only generate a hash but you can't verify the hash.


Applying the mentioned points can lead to



public static byte GenerateSaltedHash(byte plainText)

if (plainText == null) throw new ArgumentNullException("plainText");
if (plainText.Length == 0) throw new ArgumentException("Length may not be zero", "plainText");

using (HashAlgorithm algorithm = new SHA256Managed())

byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

plainText.CopyTo(saltedText, 0);
salt.CopyTo(saltedText, plainText.Length);

return algorithm.ComputeHash(saltedText);



private static byte GenerateSalt()

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())

byte salt = new byte[MaxSaltLength];
random.GetNonZeroBytes(salt);
return salt;







share|improve this answer























  • Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
    – Ioan Thomas
    May 13 at 20:31










  • IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
    – Heslacher
    May 13 at 20:35










  • @Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
    – Sinjai
    May 20 at 21:11











  • I meant overwriting the passed array.
    – Heslacher
    May 22 at 5:51












up vote
4
down vote



accepted







up vote
4
down vote



accepted






  • Because HashAlgorithm implements IDisposable you should enclose the usage of it in an using block as well.


  • Omitting braces although they might be optional can lead to hidden and therfor hard to find bugs. I would like to encourage you to always use braces.


  • Instead of using a for loop to copy plainText to saltedText and salt to saltedText you may take advantage of the Array.CopyTo() method.


  • Public methods should validate passed parameters.


  • Because you don't return the salt you can only generate a hash but you can't verify the hash.


Applying the mentioned points can lead to



public static byte GenerateSaltedHash(byte plainText)

if (plainText == null) throw new ArgumentNullException("plainText");
if (plainText.Length == 0) throw new ArgumentException("Length may not be zero", "plainText");

using (HashAlgorithm algorithm = new SHA256Managed())

byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

plainText.CopyTo(saltedText, 0);
salt.CopyTo(saltedText, plainText.Length);

return algorithm.ComputeHash(saltedText);



private static byte GenerateSalt()

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())

byte salt = new byte[MaxSaltLength];
random.GetNonZeroBytes(salt);
return salt;







share|improve this answer















  • Because HashAlgorithm implements IDisposable you should enclose the usage of it in an using block as well.


  • Omitting braces although they might be optional can lead to hidden and therfor hard to find bugs. I would like to encourage you to always use braces.


  • Instead of using a for loop to copy plainText to saltedText and salt to saltedText you may take advantage of the Array.CopyTo() method.


  • Public methods should validate passed parameters.


  • Because you don't return the salt you can only generate a hash but you can't verify the hash.


Applying the mentioned points can lead to



public static byte GenerateSaltedHash(byte plainText)

if (plainText == null) throw new ArgumentNullException("plainText");
if (plainText.Length == 0) throw new ArgumentException("Length may not be zero", "plainText");

using (HashAlgorithm algorithm = new SHA256Managed())

byte salt = GenerateSalt();
byte saltedText = new byte[plainText.Length + salt.Length];

plainText.CopyTo(saltedText, 0);
salt.CopyTo(saltedText, plainText.Length);

return algorithm.ComputeHash(saltedText);



private static byte GenerateSalt()

using (RandomNumberGenerator random = new RNGCryptoServiceProvider())

byte salt = new byte[MaxSaltLength];
random.GetNonZeroBytes(salt);
return salt;








share|improve this answer















share|improve this answer



share|improve this answer








edited May 13 at 21:14


























answered May 13 at 20:10









Heslacher

43.9k359152




43.9k359152











  • Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
    – Ioan Thomas
    May 13 at 20:31










  • IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
    – Heslacher
    May 13 at 20:35










  • @Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
    – Sinjai
    May 20 at 21:11











  • I meant overwriting the passed array.
    – Heslacher
    May 22 at 5:51
















  • Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
    – Ioan Thomas
    May 13 at 20:31










  • IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
    – Heslacher
    May 13 at 20:35










  • @Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
    – Sinjai
    May 20 at 21:11











  • I meant overwriting the passed array.
    – Heslacher
    May 22 at 5:51















Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
– Ioan Thomas
May 13 at 20:31




Thanks for the feedback. Just wondering, from a security standpoint, how safe is this method of generating salted hashes?
– Ioan Thomas
May 13 at 20:31












IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
– Heslacher
May 13 at 20:35




IMO its safe enough at least if the plainText isn't containing security relevant information. If plainText contains security relevant information you should override it inside this method.
– Heslacher
May 13 at 20:35












@Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
– Sinjai
May 20 at 21:11





@Heslacher, could you elaborate on you should override it inside this method a little, and/or show an example? I'm assumingsecurity relevant information would be passwords and such.
– Sinjai
May 20 at 21:11













I meant overwriting the passed array.
– Heslacher
May 22 at 5:51




I meant overwriting the passed array.
– Heslacher
May 22 at 5:51












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194283%2fsalted-hash-generator%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?