private void testFormat() throws IOException {
    Map<Long, byte[]> map = New.hashMap();
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(123);

    byte[] id;

    id = store.put(new ByteArrayInputStream(new byte[200]));
    assertEquals(200, store.length(id));
    assertEquals("02c8018801", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[0]));
    assertEquals("", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[1]));
    assertEquals("000100", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[3]));
    assertEquals("0003000000", StringUtils.convertBytesToHex(id));

    id = store.put(new ByteArrayInputStream(new byte[10]));
    assertEquals("010a8901", StringUtils.convertBytesToHex(id));

    byte[] combined = StringUtils.convertHexToBytes("0001aa0002bbcc");
    assertEquals(3, store.length(combined));
    InputStream in = store.get(combined);
    assertEquals(1, in.skip(1));
    assertEquals(0xbb, in.read());
    assertEquals(1, in.skip(1));
  }
Esempio n. 2
0
 private static byte[] hashPassword(boolean passwordHash, String userName, char[] password) {
   // 如果PASSWORD_HASH参数是true那么不再进行SHA256vn
   if (passwordHash) {
     return StringUtils.convertHexToBytes(new String(password));
   }
   if (userName.length() == 0 && password.length == 0) {
     return new byte[0];
   }
   // 会生成32个字节,32*8刚好是256 bit,刚好对应SHA256的名字
   return SHA256.getKeyPasswordHash(userName, password);
 }
Esempio n. 3
0
  private void testAES() {
    BlockCipher test = CipherFactory.getBlockCipher("AES");

    String r;
    byte[] data;

    // test vector from
    // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
    // ECBVarTxt128e.txt
    // COUNT = 0
    test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
    data = StringUtils.convertHexToBytes("80000000000000000000000000000000");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3ad78e726c1ec02b7ebfe92b23d9ec34", r);

    // COUNT = 127
    test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
    data = StringUtils.convertHexToBytes("ffffffffffffffffffffffffffffffff");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3f5b8cc9ea855a0afa7347d23e8d664e", r);

    // test vector
    test.setKey(StringUtils.convertHexToBytes("2b7e151628aed2a6abf7158809cf4f3c"));
    data = StringUtils.convertHexToBytes("6bc1bee22e409f96e93d7e117393172a");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3ad77bb40d7a3660a89ecaf32466ef97", r);

    test.setKey(StringUtils.convertHexToBytes("000102030405060708090A0B0C0D0E0F"));
    byte[] in = new byte[128];
    byte[] enc = new byte[128];
    test.encrypt(enc, 0, 128);
    test.decrypt(enc, 0, 128);
    if (!Arrays.equals(in, enc)) {
      throw new AssertionError();
    }

    for (int i = 0; i < 10; i++) {
      test.encrypt(in, 0, 128);
      test.decrypt(enc, 0, 128);
    }
  }
Esempio n. 4
0
 private byte[] getByteArray(Expression e) {
   return StringUtils.convertHexToBytes(e.optimize(session).getValue(session).getString());
 }