@Test public void shouldCreateHashInCorrectLength() { final String hash = Utilities.createAlphanumericHash("ABCE", 25); assertThat(hash.length(), comparesEqualTo(25)); }
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); } }
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); } } }
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(); }
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); }
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; }