MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update("hello world".getBytes()); byte[] hash = md.digest(); // calculate hash of "hello world" md.reset(); // reset the digest md.update("hello universe".getBytes()); byte[] newHash = md.digest(); // calculate hash of "hello universe"
MessageDigest md = MessageDigest.getInstance("MD5"); byte[] passwordHash = md.digest("password".getBytes());In this example, we use the MessageDigest class to calculate the MD5 hash of a password. Once the hash is calculated, we don't need to reset the digest as we have finished using it. The java.security.MessageDigest class is part of the java.security package library.