コード例 #1
0
 private void init() throws IOException {
   if (xts != null) {
     return;
   }
   this.size = base.size() - HEADER_LENGTH;
   boolean newFile = size < 0;
   byte[] salt;
   if (newFile) {
     byte[] header = Arrays.copyOf(HEADER, BLOCK_SIZE);
     salt = MathUtils.secureRandomBytes(SALT_LENGTH);
     System.arraycopy(salt, 0, header, SALT_POS, salt.length);
     DataUtils.writeFully(base, 0, ByteBuffer.wrap(header));
     size = 0;
   } else {
     salt = new byte[SALT_LENGTH];
     DataUtils.readFully(base, SALT_POS, ByteBuffer.wrap(salt));
     if ((size & BLOCK_SIZE_MASK) != 0) {
       size -= BLOCK_SIZE;
     }
   }
   AES cipher = new AES();
   cipher.setKey(SHA256.getPBKDF2(encryptionKey, salt, HASH_ITERATIONS, 16));
   encryptionKey = null;
   xts = new XTS(cipher);
 }