AcceptHash { public static String hashKey(String key) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(key.getBytes(StandardCharsets.UTF_8)); md.update(MAGIC); return new String(Base64Utils.encode(md.digest())); } catch (Exception e) { throw new EncodingAcceptHashKeyException("", e); } }  static String hashKey(String key);  }
@Test public void testHash() { byte[] key = TypeUtils.fromHexString("00112233445566778899AABBCCDDEEFF"); assertEquals(16, key.length); String clientKey = Base64.getEncoder().encodeToString(key); String serverHash = AcceptHash.hashKey(clientKey); assertEquals("mVL6JKtNRC4tluIaFAW2hhMffgE=", serverHash); }
@Test public void testRfcHashExample() { String clientKey = "dGhlIHNhbXBsZSBub25jZQ=="; String serverAccept = AcceptHash.hashKey(clientKey); String expectedHash = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="; assertEquals(expectedHash, serverAccept); }