@Override protected int compareSecure(Value v, CompareMode mode) { byte[] v2 = ((ValueBytes) v).value; if (mode.isBinaryUnsigned()) { return Bits.compareNotNullUnsigned(value, v2); } return Bits.compareNotNullSigned(value, v2); }
private void encryptBlock(byte[] in, byte[] out, int off) { int x0 = Bits.readInt(in, off); int x1 = Bits.readInt(in, off + 4); int x2 = Bits.readInt(in, off + 8); int x3 = Bits.readInt(in, off + 12); int k = key; x0 = Integer.rotateLeft(x0 ^ k, x1); x2 = Integer.rotateLeft(x2 ^ k, x1); x1 = Integer.rotateLeft(x1 ^ k, x0); x3 = Integer.rotateLeft(x3 ^ k, x0); Bits.writeInt(out, off, x0); Bits.writeInt(out, off + 4, x1); Bits.writeInt(out, off + 8, x2); Bits.writeInt(out, off + 12, x3); }
/** * Read a long value. This method reads two int values and combines them. * * @return the long value */ public long readLong() { long x = Bits.readLong(data, pos); pos += 8; return x; }
@Override protected int compareSecure(Value v, CompareMode mode) { if (type == Value.CLOB) { return Integer.signum(getString().compareTo(v.getString())); } byte[] v2 = v.getBytesNoCopy(); return Bits.compareNotNullSigned(getBytesNoCopy(), v2); }
/** * Write an integer at the current position. * The current position is incremented. * * @param x the value */ public void writeInt(int x) { Bits.writeInt(data, pos, x); pos += 4; }
/** * Read an integer at the current position. * The current position is incremented. * * @return the value */ public int readInt() { int x = Bits.readInt(data, pos); pos += 4; return x; }
@Override public byte[] getBytes() { return Bits.uuidToBytes(high, low); }
/** * Append a long value. This method writes two int values. * * @param x the value */ public void writeLong(long x) { Bits.writeLong(data, pos, x); pos += 8; }
@Override protected int compareSecure(Value v, CompareMode mode) { if (v instanceof ValueLobDb) { ValueLobDb v2 = (ValueLobDb) v; if (v == this) { return 0; } if (lobId == v2.lobId && small == null && v2.small == null) { return 0; } } if (type == Value.CLOB) { return Integer.signum(getString().compareTo(v.getString())); } byte[] v2 = v.getBytesNoCopy(); return Bits.compareNotNullSigned(getBytesNoCopy(), v2); }
/** * Update an integer at the given position. * The current position is not change. * * @param pos the position * @param x the value */ public void setInt(int pos, int x) { Bits.writeInt(data, pos, x); }
/** * Reads a long value from the byte array at the given position in big-endian * order. * * @param buff * the byte array * @param pos * the position * @return the value */ public static long readLong(byte[] buff, int pos) { return (((long) readInt(buff, pos)) << 32) + (readInt(buff, pos + 4) & 0xffffffffL); }
/** * Converts UUID value to byte array in big-endian order. * * @param uuid * UUID value * @return byte array representation */ public static byte[] uuidToBytes(UUID uuid) { return uuidToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); }
private void xorInitVector(byte[] b, int off, int len, long p) { byte[] iv = bufferForInitVector; while (len > 0) { for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i += 8) { Bits.writeLong(iv, i, (p + i) >>> 3); } cipherForInitVector.encrypt(iv, 0, Constants.FILE_BLOCK_SIZE); for (int i = 0; i < Constants.FILE_BLOCK_SIZE; i++) { b[off + i] ^= iv[i]; } p += Constants.FILE_BLOCK_SIZE; off += Constants.FILE_BLOCK_SIZE; len -= Constants.FILE_BLOCK_SIZE; } }
private void decryptBlock(byte[] in, byte[] out, int off) { int y = Bits.readInt(in, off); int z = Bits.readInt(in, off + 4); z -= (((y >>> 5) ^ (y << 4)) + y) ^ k31; y -= (((z << 4) ^ (z >>> 5)) + z) ^ k30; z -= (((y >>> 5) ^ (y << 4)) + y) ^ k1; y -= (((z << 4) ^ (z >>> 5)) + z) ^ k0; Bits.writeInt(out, off, y); Bits.writeInt(out, off + 4, z);
@Override protected int compareSecure(Value v, CompareMode mode) { Object o1 = getObject(); Object o2 = v.getObject(); boolean o1Comparable = o1 instanceof Comparable; boolean o2Comparable = o2 instanceof Comparable; if (o1Comparable && o2Comparable && Utils.haveCommonComparableSuperclass(o1.getClass(), o2.getClass())) { @SuppressWarnings("unchecked") Comparable<Object> c1 = (Comparable<Object>) o1; return c1.compareTo(o2); } // group by types if (o1.getClass() != o2.getClass()) { if (o1Comparable != o2Comparable) { return o1Comparable ? -1 : 1; } return o1.getClass().getName().compareTo(o2.getClass().getName()); } // compare hash codes int h1 = hashCode(); int h2 = v.hashCode(); if (h1 == h2) { if (o1.equals(o2)) { return 0; } return Bits.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy()); } return h1 > h2 ? 1 : -1; }
/** * Writes a long value to the byte array at the given position in big-endian * order. * * @param buff * the byte array * @param pos * the position * @param x * the value to write */ public static void writeLong(byte[] buff, int pos, long x) { writeInt(buff, pos, (int) (x >> 32)); writeInt(buff, pos + 4, (int) x); }
@Override public void setKey(byte[] b) { int[] key = new int[4]; for (int i = 0; i < 16; i += 4) { key[i / 4] = Bits.readInt(b, i); } int[] r = new int[32]; for (int i = 0, sum = 0; i < 32;) { r[i++] = sum + key[sum & 3]; sum += DELTA; r[i++] = sum + key[ (sum >>> 11) & 3]; } k0 = r[0]; k1 = r[1]; k2 = r[2]; k3 = r[3]; k4 = r[4]; k5 = r[5]; k6 = r[6]; k7 = r[7]; k8 = r[8]; k9 = r[9]; k10 = r[10]; k11 = r[11]; k12 = r[12]; k13 = r[13]; k14 = r[14]; k15 = r[15]; k16 = r[16]; k17 = r[17]; k18 = r[18]; k19 = r[19]; k20 = r[20]; k21 = r[21]; k22 = r[22]; k23 = r[23]; k24 = r[24]; k25 = r[25]; k26 = r[26]; k27 = r[27]; k28 = r[28]; k29 = r[29]; k30 = r[30]; k31 = r[31]; }
/** * Returns the value as a byte array. * * @param columnIndex (1,2,...) * @return the value */ @Override public byte[] getBytes(int columnIndex) throws SQLException { Object o = get(columnIndex); if (o == null || o instanceof byte[]) { return (byte[]) o; } if (o instanceof UUID) { return Bits.uuidToBytes((UUID) o); } return JdbcUtils.serialize(o, null); }
@Override public void setKey(byte[] key) { this.key = (int) Bits.readLong(key, 0); }
private byte[] calculateLobMac(long lobId) { if (lobMacSalt == null) { lobMacSalt = MathUtils.secureRandomBytes(LOB_MAC_SALT_LENGTH); } byte[] data = new byte[8]; Bits.writeLong(data, 0, lobId); return SHA256.getHashWithSalt(data, lobMacSalt); }