Hash String via SHA-256 in Java

To hash a string, use the built-in MessageDigest class: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.math.BigInteger; public class CryptoHash { public static void main(String[] args) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(“SHA-256”); String text = “Text to hash, cryptographically.”; // Change this to UTF-16 if needed md.update(text.getBytes(StandardCharsets.UTF_8)); byte[] digest = md.digest(); String hex = … Read more

Why are the RSA-SHA256 signatures I generate with OpenSSL and Java different?

openssl dgst -sha256 < data.txt produces something like: (stdin)= b39eaeb437e33087132f01c2abc60c6a16904ee3771cd7b0d622d01061b40729 notice the (stdin)=‘? you don’t want that to be part of your hash, if you need to create a digest, use the -binary option. try using this to sign your data: openssl sha -sha256 -sign private.pem < data.txt This does everything you need. edit – … Read more

SHA256 in swift

You have to convert explicitly between Int and CC_LONG, because Swift does not do implicit conversions, as in (Objective-)C. You also have to define hash as an array of the required size. func sha256(data : NSData) -> NSData { var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0) CC_SHA256(data.bytes, CC_LONG(data.length), &hash) let res = NSData(bytes: hash, length: … Read more

Are there any SHA-256 javascript implementations that are generally considered trustworthy? [closed]

OUTDATED: Many modern browsers now have first-class support for crypto operations. See Vitaly Zdanevich’s answer below. The Stanford JS Crypto Library contains an implementation of SHA-256. While crypto in JS isn’t really as well-vetted an endeavor as other implementation platforms, this one is at least partially developed by, and to a certain extent sponsored by, … Read more

How to hash some string with sha256 in Java?

SHA-256 isn’t an “encoding” – it’s a one-way hash. You’d basically convert the string into bytes (e.g. using text.getBytes(StandardCharsets.UTF_8)) and then hash the bytes. Note that the result of the hash would also be arbitrary binary data, and if you want to represent that in a string, you should use base64 or hex… don’t try … Read more

tech