Mercurial > hg > monetdb-java
view src/main/java/nl/cwi/monetdb/mcl/connection/helpers/ChannelSecurity.java @ 74:17365ed26611 embedded
In Java, you cannot get a pointer to a String, in order to make a faster memory copy. So I had to change a StringBuilder to a CharBuffer which allows to retrieve the pointer and make a faster processing.
author | Pedro Ferreira <pedro.ferreira@monetdbsolutions.com> |
---|---|
date | Thu, 15 Dec 2016 11:25:04 +0100 (2016-12-15) |
parents | src/main/java/nl/cwi/monetdb/mcl/connection/ChannelSecurity.java@4e2a2a81cc6a |
children | 2b5e32efb1a4 |
line wrap: on
line source
package nl.cwi.monetdb.mcl.connection.helpers; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by ferreira on 12/1/16. */ public class ChannelSecurity { private static char HexChar(int n) { return (n > 9) ? (char) ('a' + (n - 10)) : (char) ('0' + n); } /** * Small helper method to convert a byte string to a hexadecimalstring representation. * * @param digest the byte array to convert * @return the byte array as hexadecimal string */ private static String ToHex(byte[] digest) { char[] result = new char[digest.length * 2]; int pos = 0; for (byte aDigest : digest) { result[pos++] = HexChar((aDigest & 0xf0) >> 4); result[pos++] = HexChar(aDigest & 0x0f); } return new String(result); } public static String DigestStrings(String algorithm, byte[]... toDigests) { try { MessageDigest md = MessageDigest.getInstance(algorithm); for (byte[] str : toDigests) { md.update(str); } return ToHex(md.digest()); } catch (NoSuchAlgorithmException e) { throw new AssertionError("internal error: " + e.toString()); } } }