Package org.kocakosm.pitaya.util

Examples of org.kocakosm.pitaya.util.ByteBuffer


  @Test
  public void testRead() throws IOException
  {
    InputStream data = new ByteArrayInputStream(DATA);
    CountingInputStream in = new CountingInputStream(data);
    ByteBuffer buf = new ByteBuffer();
    buf.append((byte) in.read());
    byte[] b = new byte[5];
    in.read(b);
    buf.append(b);
    b = new byte[20];
    int len = in.read(b, 0, 15);
    buf.append(b, 0, len);
    assertEquals(16, in.getCount());
    assertArrayEquals(DATA, buf.toByteArray());
  }
View Full Code Here


  @Override
  public byte[] deriveKey(byte[] secret, byte[] salt)
  {
    MAC mac = Factory.getMAC(algorithm, secret);
    int d = (int) Math.ceil((double) dkLen / mac.length());
    ByteBuffer t = new ByteBuffer(d * mac.length());
    for (int i = 1; i <= d; i++) {
      byte[] f = mac.update(salt).mac(BigEndian.encode(i));
      byte[] u = f;
      for (int j = 1; j < iterationCount; j++) {
        u = mac.mac(u);
        for (int k = 0; k < f.length; k++) {
          f[k] ^= u[k];
        }
      }
      t.append(f);
    }
    return t.toByteArray(0, dkLen);
  }
View Full Code Here

  }

  private static byte[] hash(String password, byte[] salt, int r, int n, int p)
  {
    KDF scrypt = KDFs.scrypt(r, n, p, HASH_LENGTH);
    ByteBuffer buf = new ByteBuffer(HASH_LENGTH + SALT_LENGTH + 3);
    buf.append(scrypt.deriveKey(UTF8.encode(password), salt));
    buf.append(salt);
    buf.append((byte) Math.round(Math.log(n) / Math.log(2)));
    buf.append((byte) r, (byte) p);
    return buf.toByteArray();
  }
View Full Code Here

  }

  @Override
  public void nextBytes(byte[] bytes)
  {
    ByteBuffer buf = new ByteBuffer(bytes.length);
    while (buf.size() < bytes.length) {
      buf.append(src);
    }
    System.arraycopy(buf.toByteArray(), 0, bytes, 0, bytes.length);
  }
View Full Code Here

  }

  private byte[] expand(byte[] key)
  {
    MAC mac = Factory.getMAC(algorithm, key);
    ByteBuffer t = new ByteBuffer(dkLen + mac.length());
    int n = (int) Math.ceil((double) dkLen / mac.length());
    byte[] u = new byte[0];
    for (int i = 1; i <= n; i++) {
      u = mac.update(u).update(info).mac((byte) i);
      t.append(u);
    }
    return t.toByteArray(0, dkLen);
  }
View Full Code Here

  @Override
  public byte[] deriveKey(byte[] secret, byte[] salt)
  {
    KDF pbkdf2 = KDFs.pbkdf2(Algorithm.HMAC_SHA256, 1, p * 128 * r);
    byte[] b = pbkdf2.deriveKey(secret, salt);
    ByteBuffer buffer = new ByteBuffer(p * 128 * r);
    for (int i = 0; i < p; i++) {
      buffer.append(roMix(slice(b, i * 128 * r, 128 * r)));
    }
    pbkdf2 = KDFs.pbkdf2(Algorithm.HMAC_SHA256, 1, dkLen);
    return pbkdf2.deriveKey(secret, buffer.toByteArray());
  }
View Full Code Here

  }

  private byte[] roMix(byte[] x)
  {
    int len = x.length;
    ByteBuffer v = new ByteBuffer(n * len);
    for (int i = 0; i < n; i++) {
      v.append(x);
      x = blockMix(x);
    }
    int offset = (2 * r - 1) * 64;
    for (int i = 0; i < n; i++) {
      int j = LittleEndian.decodeInt(x, offset) & (n - 1);
      x = blockMix(xor(x, v.toByteArray(j * len, len)));
    }
    return x;
  }
View Full Code Here

  }

  private byte[] blockMix(byte[] in)
  {
    byte[] x = slice(in, (2 * r - 1) * 64, 64);
    ByteBuffer buffer = new ByteBuffer(128 * r);
    for (int i = 0; i < 2 * r; i++) {
      x = salsa20(xor(x, slice(in, i * 64, 64)), 8);
      buffer.append(x);
    }
    byte[] y = buffer.toByteArray();
    byte[] b = new byte[in.length];
    for (int i = 0; i < r; i++) {
      System.arraycopy(y, (i * 2) * 64, b, i * 64, 64);
    }
    for (int i = 0; i < r; i++) {
View Full Code Here

TOP

Related Classes of org.kocakosm.pitaya.util.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.