Sha256 { public static byte[] hash(String text) { if (text == null) { throw new IllegalArgumentException("Text must not be null"); } try { return hash(text.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new UnsupportedOperationException("The encoding 'UTF-8' is not supported", e); } } private  Sha256(); static byte[] hash(String text); static byte[] hash(byte[] bytes); static byte[] hash(byte[] bytes, int offset, int length);  }
@Test public void whenInputIsStringOutputShouldBeHashed() { byte[] hash = Sha256.hash("test"); Assert.assertTrue("Hash must be 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", Arrays.equals(ByteUtils.hexStringToByteArray("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"), hash)); }
@Test(expected = IllegalArgumentException.class) public void whenInputStringIsNullShouldThrowArgumentException() { String input = null; Sha256.hash(input); }
@Test public void whenInputIsByteOutputShouldBeHashed() { byte[] hash = Sha256.hash(ByteUtils.hexStringToByteArray("afc47cff")); Assert.assertTrue("Hash must be e3f4a8d4eeedd1cada0c4cf2e2f457343a18544160292aa83e3b8d59f23bc375", Arrays.equals(ByteUtils.hexStringToByteArray("e3f4a8d4eeedd1cada0c4cf2e2f457343a18544160292aa83e3b8d59f23bc375"), hash)); }
@Test(expected = IllegalArgumentException.class) public void whenInputBytesIsNullShouldThrowArgumentException() { byte[] input = null; Sha256.hash(input); }