Example usage for java.nio ByteBuffer putLong

List of usage examples for java.nio ByteBuffer putLong

Introduction

In this page you can find the example usage for java.nio ByteBuffer putLong.

Prototype

public abstract ByteBuffer putLong(long value);

Source Link

Document

Writes the given long to the current position and increases the position by 8.

Usage

From source file:org.linguafranca.pwdb.kdbx.dom.DomHelper.java

static String base64FromUuid(UUID uuid) {
    byte[] buffer = new byte[16];
    ByteBuffer b = ByteBuffer.wrap(buffer);
    b.putLong(uuid.getMostSignificantBits());
    b.putLong(8, uuid.getLeastSignificantBits());
    // round the houses for Android
    return new String(Base64.encodeBase64(buffer));
}

From source file:org.linguafranca.pwdb.kdbx.dom.DomHelper.java

static String hexStringFromUuid(UUID uuid) {
    byte[] buffer = new byte[16];
    ByteBuffer b = ByteBuffer.wrap(buffer);
    b.putLong(uuid.getMostSignificantBits());
    b.putLong(8, uuid.getLeastSignificantBits());
    // round the houses for Android
    return new String(Hex.encodeHex(buffer));
}

From source file:org.janusgraph.TestByteBuffer.java

private static long testByte() {
    LongObjectMap<ConcurrentSkipListSet<ByteEntry>> tx = new LongObjectHashMap<ConcurrentSkipListSet<ByteEntry>>(
            NUM);// w w w  .j  a v a 2  s.co m
    for (int i = 0; i < NUM; i++) {
        tx.put(i, new ConcurrentSkipListSet<ByteEntry>());
    }
    for (int i = 0; i < NUM; i++) {
        for (int j = 0; j < NUM; j++) {
            if (i == j)
                continue;
            if (Math.random() < FRACTION) {
                ByteBuffer key = ByteBuffer.allocate(16);
                key.putLong(5).putLong(j).flip();
                ByteBuffer value = ByteBuffer.allocate(4);
                value.putInt(random.nextInt(ROUNDSIZE)).flip();
                tx.get(i).add(new ByteEntry(key, value));
            }
        }
    }
    long time = System.currentTimeMillis();
    long sum = 0;
    for (int t = 0; t < TRIALS; t++) {
        for (int i = 0; i < NUM; i++) {
            for (Vertex v : (new ByteVertex(i, tx)).getNeighbors(0)) {
                sum += v.getId();
            }
        }
    }
    time = System.currentTimeMillis() - time;
    return time;
}

From source file:com.scf.utils.UUIDUtilies.java

protected static String base64Uuid(UUID uuid) {

    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());

    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:com.scf.utils.UUIDUtilies.java

protected static String base58Uuid(UUID uuid) {

    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());

    return Base58.encode(bb.array());
}

From source file:org.asynchttpclient.ntlm.NtlmTest.java

private static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.putLong(x);
    return buffer.array();
}

From source file:org.wso2.carbon.identity.application.authenticator.totp.TOTPTokenGenerator.java

/**
 * Create the TOTP token for a given secret key and time index.
 *
 * @param secret    Secret key in binary format
 * @param timeIndex Number of Time elapse from the unix epoch time
 * @return TOTP token value as a long//from  w ww. ja  v  a  2 s .c  o  m
 * @throws NoSuchAlgorithmException Could not find the specific algorithm
 * @throws InvalidKeyException      invalid signKey
 */
private static long getCode(byte[] secret, long timeIndex)
        throws NoSuchAlgorithmException, InvalidKeyException {
    // Building the secret key specification for the HmacSHA1 algorithm.
    SecretKeySpec signKey = new SecretKeySpec(secret, TOTPAuthenticatorConstants.HMAC_ALGORITHM);
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(timeIndex);
    byte[] timeBytes = buffer.array();
    // Getting an HmacSHA1 algorithm implementation from the Java Cryptography Extension (JCE).
    Mac mac = Mac.getInstance(TOTPAuthenticatorConstants.HMAC_ALGORITHM);
    // Initializing the MAC algorithm.
    mac.init(signKey);
    // Processing the instant of time and getting the encrypted data.
    byte[] hash = mac.doFinal(timeBytes);
    // Building the validation code performing dynamic truncation.
    int offset = hash[19] & 0xf;
    long truncatedHash = hash[offset] & 0x7f;
    for (int i = 1; i < 4; i++) {
        truncatedHash <<= 8;
        truncatedHash |= hash[offset + i] & 0xff;
    }
    truncatedHash %= 1000000;
    return truncatedHash;
}

From source file:Main.java

static byte[] decryptJWE(String jwe, Key privRsaKey) {
    // Log.d("","decryptJWE");

    try {//from w w w .ja  va 2s. c o m
        // split jwe string
        StringTokenizer tokens = new StringTokenizer(jwe, ".");
        int count = tokens.countTokens();
        // Log.d("","parts.length: "+count);

        if (count != 5)
            return null;

        String jweProtectedHeader64 = tokens.nextToken();
        String jweEncrypted64 = tokens.nextToken();
        String jweInitVector64 = tokens.nextToken();
        String cryptedBytes64 = tokens.nextToken();
        String auth_tag64 = tokens.nextToken();

        // decrypt cek using private rsa key
        byte[] cek = decryptRsaB64(jweEncrypted64, privRsaKey);

        // check cek result byte array
        if (cek == null || cek.length == 0 || (cek.length % 2) != 0)
            return null;

        int keySize = cek.length / 2;
        Log.d("", "Decryption AES: " + keySize * 8);

        // build aes_key and hmac_key
        byte aes_key[] = new byte[keySize];
        byte hmac_key[] = new byte[keySize];

        System.arraycopy(cek, 0, hmac_key, 0, keySize);
        System.arraycopy(cek, keySize, aes_key, 0, keySize);

        // decode initialization vector
        byte[] iv_key = decodeB64(jweInitVector64);

        Log.d("", "hmac_key: " + bytesToHex(hmac_key));
        Log.d("", "aes_key:  " + bytesToHex(aes_key));
        Log.d("", "iv_key:   " + bytesToHex(iv_key));

        // decrypt content using aes_key and iv_key
        byte[] cryptedBytes = decodeB64(cryptedBytes64);
        Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding", "SC");
        decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key));
        byte[] decryptedBytes = decrypt.doFinal(cryptedBytes);

        Log.d("", "decryptedBytes:");
        Log.d("", bytesToHex(decryptedBytes));

        // validation verification
        byte[] aad = jweProtectedHeader64.getBytes();
        long al = aad.length * 8;

        // concatenate aad, iv_key, cryptedBytes and al 
        byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8];
        int offset = 0;
        System.arraycopy(aad, offset, hmacData, 0, aad.length);
        offset += aad.length;
        System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length);
        offset += iv_key.length;
        System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length);
        offset += cryptedBytes.length;
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(al);
        System.arraycopy(buffer.array(), 0, hmacData, offset, 8);

        // compute hmac
        Mac hmac = Mac.getInstance("HmacSHA256", "SC");
        hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));
        byte[] hmacValue = hmac.doFinal(hmacData);

        // pick authentication tag
        byte[] authTag = Arrays.copyOf(hmacValue, 16);

        // validate authentication tag
        byte[] authTagRead = decodeB64(auth_tag64);
        for (int i = 0; i < 16; i++) {
            if (authTag[i] != authTagRead[i]) {
                Log.d("", "validation failed");
                return decryptedBytes;
            }
        }

        Log.d("", "validation success");

        // validation success
        return decryptedBytes;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:ec.edu.chyc.manejopersonal.util.ServerUtils.java

/***
 * Devuelve un UUID generado y codificado en Base64
 *
 * @return Uuid codificado en Base64/*  www.  j  a  va  2  s  .  co  m*/
 */
public static String generateB64Uuid() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer uuidBytes = ByteBuffer.wrap(new byte[16]);
    uuidBytes.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    String asB64 = Base64.getEncoder().encodeToString(uuidBytes.array());
    return asB64;
}

From source file:info.gehrels.flockDBClient.ByteHelper.java

static ByteBuffer asByteBufferOrNull(long... destinationIds) {
    ByteBuffer buf = null;
    if (isNotEmpty(destinationIds)) {
        buf = ByteBuffer.wrap(new byte[destinationIds.length * (Long.SIZE / 8)]);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        for (long destinationId : destinationIds) {
            buf.putLong(destinationId);
        }/*from   w  w  w  .  j a  v  a  2s .  c o  m*/
        buf.rewind();
    }
    return buf;
}