Saturday, 15 August 2015

encryption - Extract Salt From Password C# -



encryption - Extract Salt From Password C# -

this question has reply here:

bitconverter.tostring() in reverse? [duplicate] 7 answers

i have function calculate sha256 hash salt. salt appended onto hashed password , stored. function doing looks so:

public static string calculatehash(string input) { var inputbuffer = new list<byte>(encoding.unicode.getbytes(input)); var saltbytes = new byte[16]; using (var rnd = randomnumbergenerator.create()) { rnd.getbytes(saltbytes); } inputbuffer.addrange(saltbytes); byte[] hashedbytes; using (var hasher = new sha256managed()) { hashedbytes = hasher.computehash(inputbuffer.toarray()); } var hash = bitconverter.tostring(hashedbytes).replace("-", string.empty); var salt = bitconverter.tostring(saltbytes).replace("-", string.empty); homecoming string.format("{0}:{1}", hash, salt); }

it stores string of 97 characters (including ':') in length , seems work well. struggling strip salt off of hash when retrieve back. issue having converting salt, in string form, byte[16], contains original bytes. assume 1 time have these original 16 bytes, can append them user input, , hash passwords check match.

my current effort split hashed password , salt @ colon delimiter , utilize getbytes function. works this:

public static bool validatepassword(string password, string hashwithsalt) { var split = hashwithsalt.split(':'); var salt = split[1]; var saltbytes = getbytes(salt); } private static byte[] getbytes(string str) { var bytes = new byte[str.length * sizeof(char)]; buffer.blockcopy(str.tochararray(), 0, bytes, 0, bytes.length); homecoming bytes; }

but getbytes returning byte[64], not original byte format. how can go fixing this?

figured out own solution. needed alter getbytes function handle hex conversion.

private static byte[] getbytes(string str) { homecoming enumerable.range(0, str.length) .where(x => x % 2 == 0) .select(x => convert.tobyte(str.substring(x, 2), 16)) .toarray(); }

c# encryption hash

No comments:

Post a Comment