Home >Java >javaTutorial >How to Hash a String with SHA-256 in Java: A Step-by-Step Guide
Hashing a String with SHA-256 in Java: Unraveling the Encoding Enigma
Hashing algorithms, like SHA-256, are not encoding mechanisms; they're one-way irreversible functions. To hash a string with SHA-256 in Java, follow these steps:
For a practical example:
<code class="java">MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));</code>
This snippet demonstrates how to obtain the SHA-256 hash of a string in Java. Remember that hashing provides irreversible data transformation, typically used for cryptographic purposes or ensuring data integrity.
The above is the detailed content of How to Hash a String with SHA-256 in Java: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!