-2

Username ABC
Password DEF
Salt = Username + Password IE; ABCDEF(saltkey)

Now, this salt key is hashed using SHA256 algorithm and the output for the saltkey(ABCDEF) after hashing it is: jjaiBx04mbD1ZvsLKuG6PyBFfJcYbl7iCnDCsi2l4tk=

Now how do I truncate this to a smaller hash so that the result is just the first 10 characters of it let say: jjaiBx04mb

krishna P
  • 19
  • 2
  • 4
  • 1
    This sounds a lot like an [XY problem](http://xyproblem.info/) to me. – Uwe Keim Jul 27 '17 at 17:32
  • 1
    Side note: technically SHA256 does not return strings but rather byte arrays... but since post not really concerned about source of the string it does not matter too much. – Alexei Levenkov Jul 27 '17 at 17:42

1 Answers1

2

You can use String.Substring:

int start = 0;
int length = 10;
string result = myString.Substring(start, length);

This will return the substring of the original string that starts at position start and has length length.

Therefore, giving start a value of 0 will yield the first length characters of the original string.

However, if I may make a suggestion, assuming that it is within your control, do not create salts this way. Use a completely random sequence of characters, preferably created using a secure random generator, and store it alongside the username and hashed password. Also, do make your salt fairly long. I'm sure you can spare the extra few bytes of DB space it will require.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53