Skip to content

Instantly share code, notes, and snippets.

@selaromi
Created February 22, 2015 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save selaromi/e6f253d739bb31c95853 to your computer and use it in GitHub Desktop.
Save selaromi/e6f253d739bb31c95853 to your computer and use it in GitHub Desktop.
SHA256 in Java Ruby C# PHP Phython
*SHA256–Bit Hash Java Example*
import java.security.MessageDigest;
import java.security.SignatureException;
String sourceString = ...; // shared secret + fields in correct format
String hash = sha256Digest(sourceString);
public String sha256Digest (String data) throws SignatureException {
return getDigest(“SHA-256”, data, true);
}
private String getDigest(String algorithm, String data, boolean toLower)
throws SignatureException {
try {
MessageDigest mac = MessageDigest.getInstance(algorithm);
mac.update(data.getBytes(“UTF-8”));
return toLower ?
new String(toHex(mac.digest())).toLowerCase() : new String(toHex(mac.digest()));
} catch (Exception e) {
throw new SignatureException(e);
}
}
private String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format(“%0” + (bytes.length << 1) + “X”, bi);
}
Note: org.apache.commons.code.digest is a useful library for generating the SHA256 hash.
*SHA256–Bit Hash PHP Example*
$hash = hash('sha256',$sourceString);
*SHA256–Bit Hash Ruby Example*
require ‘digest’
hash = Digest::SHA256.hexdigest(sourceString)
*SHA256–Bit Hash Python Example*
hash = hashlib.sha256(sourceString).hexdigest()
*SHA256–Bit Hash C# Example*
import System.Security.Cryptography.SHA256;
public string GetHashSha256(string text)
{
byte[] bytes = Encoding.ASCII.GetBytes(text);
SHA256Managed hashstring = new SHA256Managed();
byte[] hash = hashstring.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += String.Format("{0:x2}", x);
}
return hashString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment