Example usage for io.netty.buffer ByteBuf getBytes

List of usage examples for io.netty.buffer ByteBuf getBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getBytes.

Prototype

public abstract int getBytes(int index, FileChannel out, long position, int length) throws IOException;

Source Link

Document

Transfers this buffer's data starting at the specified absolute index to the specified channel starting at the given file position.

Usage

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private String parseUser(int offset, ByteBuf buf) {
    int userLength = buf.getUnsignedShort(offset);
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 2, userBytes, 0, userLength);
    return new String(userBytes);
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private byte[] parseIv(int offset, ByteBuf buf) {
    byte[] iv = new byte[16];
    buf.getBytes(offset, iv, 0, 16);
    return iv;// w  w  w  . jav  a2 s.  c  o  m
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private boolean verifySignature(int offset, int length, ByteBuf buf) throws OnRecordErrorException {
    boolean isVerified = false;
    if (securityLevel == SecurityLevel.NONE) {
        return true;
    }/* www  .  j a v a2s  . c  o  m*/

    if (length < 33) {
        LOG.warn("No username");
    } else if (length < 32) {
        LOG.warn("invalid signature");
    }

    byte[] signature = new byte[32];
    buf.getBytes(offset, signature, 0, 32);

    int userLength = length - offset - 32;
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 32, userBytes, 0, userLength);
    String username = new String(userBytes);

    if (!authKeys.containsKey(username)) {
        throw new OnRecordErrorException(Errors.COLLECTD_03,
                "Auth File doesn't contain requested user: " + username);
    }
    String key = authKeys.get(username);

    try {
        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(charset), "HmacSHA256");
        sha256HMAC.init(secretKey);

        int userPayloadLength = buf.capacity() - length + username.length();
        byte[] userPayload = new byte[userPayloadLength];
        buf.getBytes(offset + 32, userPayload, 0, userPayloadLength);
        isVerified = Arrays.equals(sha256HMAC.doFinal(userPayload), signature);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new OnRecordErrorException(Errors.COLLECTD_02, e.toString());
    }
    return isVerified;
}

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

private void decrypt(int offset, int length, ByteBuf buf, String user, byte[] iv)
        throws OnRecordErrorException {
    int contentLength = length - offset;
    if (contentLength < 26) {
        throw new OnRecordErrorException(Errors.COLLECTD_03, "Content Length was: " + contentLength);
    }/*from   w ww  .ja va2s.co  m*/

    if (!authKeys.containsKey(user)) {
        throw new OnRecordErrorException(Errors.COLLECTD_03,
                "Auth File doesn't contain requested user: " + user);
    }
    String key = authKeys.get(user);

    try {
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        sha256.update(key.getBytes(charset));

        Cipher cipher = Cipher.getInstance("AES/OFB/NoPadding");

        SecretKeySpec keySpec = new SecretKeySpec(sha256.digest(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        byte[] encrypted = new byte[contentLength];
        buf.getBytes(offset, encrypted, 0, contentLength);
        byte[] decrypted = cipher.doFinal(encrypted);

        if (!verifySha1Sum(decrypted)) {
            throw new OnRecordErrorException(Errors.COLLECTD_03, "SHA-1 Checksum Failed");
        }

        buf.setBytes(offset, decrypted);
    } catch (GeneralSecurityException e) {
        throw new OnRecordErrorException(Errors.COLLECTD_03, e.toString());
    }
}

From source file:com.streamsets.pipeline.lib.parser.udp.collectd.CollectdParser.java

License:Apache License

private String parseString(int offset, int length, ByteBuf buf) {
    // N-bytes//  ww  w  . j a va2 s  .c  o  m
    byte[] bytes = new byte[length - 5];
    buf.getBytes(offset, bytes, 0, length - 5);
    return new String(bytes, StandardCharsets.UTF_8);
}

From source file:com.streamsets.pipeline.lib.parser.udp.collectd.CollectdParser.java

License:Apache License

private String parseUser(int offset, ByteBuf buf) {
    int userLength = buf.getUnsignedShort(offset);
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 2, userBytes, 0, userLength);
    return new String(userBytes, StandardCharsets.UTF_8);
}

From source file:com.streamsets.pipeline.lib.parser.udp.collectd.CollectdParser.java

License:Apache License

private boolean verifySignature(int offset, int length, ByteBuf buf) throws OnRecordErrorException {
    boolean isVerified = false;
    if (securityLevel == SecurityLevel.NONE) {
        return true;
    }//from  w  w w.  jav  a 2  s  . co m

    if (length < 33) {
        LOG.warn("No username");
    }

    if (length < 32) {
        LOG.warn("invalid signature");
    }

    byte[] signature = new byte[32];
    buf.getBytes(offset, signature, 0, 32);

    int userLength = length - offset - 32;
    byte[] userBytes = new byte[userLength];
    buf.getBytes(offset + 32, userBytes, 0, userLength);
    String username = new String(userBytes, StandardCharsets.UTF_8);

    if (!authKeys.containsKey(username)) {
        throw new OnRecordErrorException(Errors.COLLECTD_03,
                "Auth File doesn't contain requested user: " + username);
    }
    String key = authKeys.get(username);

    try {
        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(charset), "HmacSHA256");
        sha256HMAC.init(secretKey);

        int userPayloadLength = buf.capacity() - length + username.length();
        byte[] userPayload = new byte[userPayloadLength];
        buf.getBytes(offset + 32, userPayload, 0, userPayloadLength);
        isVerified = Arrays.equals(sha256HMAC.doFinal(userPayload), signature);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new OnRecordErrorException(Errors.COLLECTD_02, e.toString());
    }
    return isVerified;
}

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

public void decodeBody(ByteBuf _byteBuf) {
    byte[] bs = new byte[] { -117, -93, 2, 0, 0, 0, 0, 64, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 114, 111, 111, 116, 0, 20, -19, -111, -3, 39, -46, -116, -128, -44, -112, -26,
            -48, 42, 70, -85, 8, 83, 83, 100, 103, 68, 116, 97, 108, 101, 110, 116, 95, 98, 97, 115, 101, 119,
            101, 98, 50, 48, 49, 0 };/*from  w ww.java  2 s. c  om*/
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    byteBuf.setBytes(0, bs, 0, bs.length);

    int _index = byteBuf.readerIndex();
    int index = _index;

    clientFlags = byteBuf.getInt(index); //172939
    index += 4;

    maxPacketSize = byteBuf.getInt(index); //1073741824
    index += 4;

    charsetIndex = byteBuf.getByte(index); //33
    index += 1;

    index += extra.length;

    int len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    user = new byte[len];
    byteBuf.getBytes(index, user, 0, len);
    index += len;
    index++;

    passwordLen = byteBuf.getByte(index);
    index += 1;

    password = new byte[passwordLen];
    byteBuf.getBytes(index, password, 0, passwordLen);

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    database = new byte[len];
    byteBuf.getBytes(index, database, 0, len);
    index += len;
    index++;

}

From source file:com.talent.mysql.packet.response.HandshakePacket.java

License:Open Source License

@Override
public HandshakePacket decodeBody(ByteBuf byteBuf, MysqlHeader mysqlHeader) throws DecodeException {
    this.setMysqlHeader(mysqlHeader);
    int _index = byteBuf.readerIndex();
    int index = _index;

    protocolVersion = byteBuf.getByte(index++);

    int len = 0;/*from   www .  ja  v  a  2  s .c  o m*/
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    versionInfo = new byte[len];
    byteBuf.getBytes(index, versionInfo, 0, len);
    index += len;
    index++;

    threadId = byteBuf.getInt(index);
    index += 4;

    encrypt1 = new byte[8];
    byteBuf.getBytes(index, encrypt1, 0, 8);
    index += 8;

    fix1 = byteBuf.getByte(index++);

    serverProp1 = new byte[2];
    byteBuf.getBytes(index, serverProp1, 0, 2);
    index += 2;

    charset = byteBuf.getByte(index++);

    serverStatus = new byte[2];
    byteBuf.getBytes(index, serverStatus, 0, 2);
    index += 2;

    serverProp2 = new byte[2];
    byteBuf.getBytes(index, serverProp2, 0, 2);
    index += 2;

    fix2 = byteBuf.getByte(index++);

    //      byte10 = new byte[10];
    //      byteBuf.getBytes(index, byte10, 0, 10);
    index += 10;

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    encrypt2 = new byte[len];
    byteBuf.getBytes(index, encrypt2, 0, len);
    index += len;
    index++;

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    authPluginName = new byte[len];
    byteBuf.getBytes(index, authPluginName, 0, len);
    index += len;
    index++;

    byteBuf.readerIndex(index);
    return this;
}

From source file:com.tesora.dve.db.mysql.libmy.MyLoginRequest.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    clientCapabilities = cb.readUnsignedInt();
    boolean hasConnectDatabase = ((clientCapabilities
            & ClientCapabilities.CLIENT_CONNECT_WITH_DB) == ClientCapabilities.CLIENT_CONNECT_WITH_DB);
    maxPacketSize = cb.readInt();//from  www. j a v  a2 s . com
    clientCharset = cb.readByte();

    cb.skipBytes(23); // login request has a 23 byte filler
    username = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
    cb.skipBytes(1); // skip the NULL terminator
    byte passwordLength = cb.readByte();
    byte[] passwordBytes = new byte[passwordLength];
    cb.getBytes(cb.readerIndex(), passwordBytes, 0, passwordLength);
    password = new String(passwordBytes, CharsetUtil.ISO_8859_1);
    cb.skipBytes(passwordLength);
    // if the clientCapabilities flag has the CLIENT_CONNECT_WITH_DB bit set,
    // then this message contains an initial database to connect to
    if (hasConnectDatabase) {
        database = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
        if (database.length() < 1) {
            database = null;
        }
    }
}