MessageDigest.getInstance(algorithm) always throwing NoSuchAlgorithmException
<p>I'm creating a simple password vault/generator, and right now I'm working on the vault portion of my code, and am writing code for encrypting passwords. I'm using MessageDigest.</p>

<p>However when I try to choose an algorithm it always throws a NoSuchAlgorithmException. I'm trying to use SHA-1 or SHA-256. I've looked everywhere and I can't find a solution. EDIT: I forgot to mention I've tried "SHA-1", "SHA-256", and "MD5". None work.</p>

<p>My method:</p>

<pre><code>private static String generateHash(String input) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    messageDigest.update(input.getBytes());
    String encryptedString = new String(messageDigest.digest());
    System.out.println("encryptedString :: " + encryptedString);
    return encryptedString;
}
</code></pre>

<p>(I'm only printing out encryptedString for debugging purposes)</p>

<p>I'm using JDK 1.8.0_91, and IntelliJ IDEA 1.5.0.2.</p>

<p><strong>EDIT:</strong> So I figured out what was wrong. Instead of using <code>throws NoSuchAlgorithmException</code>, I need to put my code into a try/catch block catching <code>NoSuchAlgorithmException</code>. I don't know why but it fixed my issue.</p>