Created
June 2, 2015 19:17
-
-
Save ericjsilva/86bb8f2600060a5f4510 to your computer and use it in GitHub Desktop.
SHA-256 Hash algorithms to test portability across platforms
This file contains 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
var crypto = require("crypto"); | |
var sha256 = crypto.createHash("sha256"); | |
var pwd = "hcptest"; | |
var salt = "7T0m4rKV5KXEaAL2PmtI8Rs9ehdM9csPOnQVgHe3U9A="; | |
var text2hash = pwd + salt; | |
var expected = "lDu2dH6lytKxG3MzCRWAjdXha03/WckYcc08nh0ZPBQ="; | |
sha256.update(text2hash, "utf8"); //utf8 here | |
var result = sha256.digest("base64"); | |
console.log('Result:' + result); | |
console.log('Excpected:' + expected); |
This file contains 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
<?php | |
$pwd = "hcptest"; | |
$salt = "7T0m4rKV5KXEaAL2PmtI8Rs9ehdM9csPOnQVgHe3U9A="; | |
$expected = "lDu2dH6lytKxG3MzCRWAjdXha03/WckYcc08nh0ZPBQ="; | |
$token = hash("sha256", $pwd.$salt, true); | |
echo "\nText to hash: " . $text2hash; | |
echo "\nYour token is: " . $token; | |
echo "\nBase 64 string is: " . base64_encode($token); | |
echo "\nExpected: " . $expected; | |
?> |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Security.Cryptography; | |
using System.Text; | |
public class SHA256Test | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Result: " + HashPassword("hcptest", "7T0m4rKV5KXEaAL2PmtI8Rs9ehdM9csPOnQVgHe3U9A=")); | |
Console.WriteLine("Expected: lDu2dH6lytKxG3MzCRWAjdXha03/WckYcc08nh0ZPBQ="); | |
} | |
public static bool CheckPassword(string password, string salt, string hash) | |
{ | |
return HashPassword(password, salt) == hash; | |
} | |
public static string HashPassword(string password, string salt) | |
{ | |
string combined = password + salt; | |
return HashString(combined); | |
} | |
public static string CreateSalt(int size) | |
{ | |
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | |
byte[] buff = new byte[size]; | |
rng.GetBytes(buff); | |
return Convert.ToBase64String(buff); | |
} | |
private static string HashString(string toHash) | |
{ | |
using (SHA256 sha = SHA256.Create()) | |
{ | |
byte[] dataToHash = Encoding.UTF8.GetBytes(toHash); | |
byte[] hashed = sha.ComputeHash(dataToHash); | |
return Convert.ToBase64String(hashed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment