Example usage for java.nio CharBuffer array

List of usage examples for java.nio CharBuffer array

Introduction

In this page you can find the example usage for java.nio CharBuffer array.

Prototype

public final char[] array() 

Source Link

Document

Returns the char array which this buffer is based on, if there is one.

Usage

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator.java

private String generateToken(final String username, final String salt, final String time) {
    try {/*from ww w  .  ja  v  a 2s . co m*/
        final CharBuffer secretAndSalt = CharBuffer.allocate(secret.length + salt.length() + 1);
        secretAndSalt.put(secret);
        secretAndSalt.put(":");
        secretAndSalt.put(salt);
        final String tokenPrefix = username + ":" + time.toString() + ":";
        final SecretKeySpec keySpec = new SecretKeySpec(toBytes(secretAndSalt.array()), hmacAlgo);
        final Mac hmac = Mac.getInstance(hmacAlgo);
        hmac.init(keySpec);
        hmac.update(username.getBytes());
        hmac.update(time.toString().getBytes());
        final Base64.Encoder encoder = Base64.getUrlEncoder();
        final byte[] hmacbytes = encoder.encode(hmac.doFinal());
        final byte[] tokenbytes = tokenPrefix.getBytes();
        final byte[] token = ByteBuffer.wrap(new byte[tokenbytes.length + hmacbytes.length]).put(tokenbytes)
                .put(hmacbytes).array();
        return new String(encoder.encode(token));
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.HMACAuthenticator.java

private byte[] toBytes(char[] chars) {
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
    byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
    Arrays.fill(charBuffer.array(), '\u0000'); //Clear sensitive data from memory
    Arrays.fill(byteBuffer.array(), (byte) 0); //Clear sensitive data from memory
    return bytes;
}

From source file:org.nuxeo.common.codec.Crypto.java

/**
 * Utility method to get {@code char[]} from {@code bytes[]} since it is recommended to store passwords in
 * {@code char[]} rather than in {@code String}.<br>
 * The default charset of this Java virtual machine is used. There can be conversion issue with unmappable
 * characters: they will be replaced with the charset's default replacement string.
 *
 * @param bytes byte array to convert/* ww w  .  ja v a  2 s .  c om*/
 * @return the char array converted from {@code bytes} using the default charset.
 */
public static char[] getChars(byte[] bytes) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    CharBuffer charBuffer = Charset.defaultCharset().decode(byteBuffer);
    return Arrays.copyOfRange(charBuffer.array(), 0, charBuffer.limit());
}

From source file:sh.isaac.provider.sync.git.gitblit.utils.ConnectionUtils.java

/**
 * To bytes.//w w w  .ja v a 2 s .  c o  m
 *
 * @param chars the chars
 * @return the byte[]
 */
private static byte[] toBytes(char[] chars) {
    final CharBuffer charBuffer = CharBuffer.wrap(chars);
    final ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
    final byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());

    Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
    Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
    return bytes;
}