### Question:
Utilities { public static String createAlphanumericHash(final String str, final int length) { try { final MessageDigest md = MessageDigest.getInstance("SHA-256"); final byte[] digest = md.digest(str.getBytes(CHARSET_UTF8)); final StringBuilder sb = new StringBuilder(); for (final byte b : digest) { if (b < 16) sb.append("0"); sb.append(Integer.toHexString(b & 0xff)); } while (sb.length() < length) { sb.append(sb.toString()); } return sb.delete(length, sb.length()).toString(); } catch (NoSuchAlgorithmException | UnsupportedCharsetException e) { return createRandomAlphanumeric(length); } } private  Utilities(); static boolean isNullOrEmptyString(final String value); static String createRandomAlphanumeric(final int length); static String createAlphanumericHash(final String str, final int length); static void generateInvocationIdIfNecessary(final TraceeBackend backend); static void generateSessionIdIfNecessary(final TraceeBackend backend, final String sessionId); static final Charset CHARSET_UTF8; }### Answer:
@Test public void shouldCreateSameHashForSameString() { final String hashA = Utilities.createAlphanumericHash("ABC", 20); final String hashB = Utilities.createAlphanumericHash("ABC", 20); assertThat(hashA, equalTo(hashB)); }
@Test public void shouldCreateDifferentHashForDifferentString() { final String hashA = Utilities.createAlphanumericHash("ABCE", 20); final String hashB = Utilities.createAlphanumericHash("ABCD", 20); assertThat(hashA, not(equalTo(hashB))); }
@Test public void shouldCreateHashInCorrectLength() { final String hash = Utilities.createAlphanumericHash("ABCE", 25); assertThat(hash.length(), comparesEqualTo(25)); }
@Test public void shouldRepeatHashesIfLengthIsToLong() { final String hash = Utilities.createAlphanumericHash("ABCD", 500); assertThat(hash.length(), comparesEqualTo(500)); }
@Test public void shouldHandleNormalSha256Length() { final String hash = Utilities.createAlphanumericHash("ABCD", 77); assertThat(hash.length(), comparesEqualTo(77)); }