Ejemplo n.º 1
0
 /**
  * Converts a token to a sequence of codepoints.
  *
  * @param token token
  * @return codepoints
  */
 public static int[] cps(final byte[] token) {
   int pos = 0;
   final int len = token.length;
   final int[] cp = new int[len];
   for (int i = 0; i < len; i += cl(token, i)) cp[pos++] = cp(token, i);
   return pos < len ? Arrays.copyOf(cp, pos) : cp;
 }
Ejemplo n.º 2
0
  /**
   * Ecrit la piece donnée dans le fichier temporaire sur le disque
   *
   * @param piece : pièce à écrire
   * @param num : numéros de la pièce
   */
  private synchronized void writePieceTmpFile(byte[] piece, int num) {
    if (num < 0 || num >= this.nbPieces()) {
      throw new IllegalArgumentException();
    }
    if (piece.length > _piecesize) {
      throw new IllegalArgumentException();
    }

    try {
      RandomAccessFile writer_tmp = new RandomAccessFile(this, "rw");
      FileChannel writer = writer_tmp.getChannel();

      int index_piece = ((int) this.length() - this.headerSize()) / _piecesize;

      if (piece.length < _piecesize) {
        piece = Arrays.copyOf(piece, _piecesize);
      }
      Tools.write(writer, 4 + _key.length() + 4 + 4 + 4 * num, index_piece);
      Tools.write(writer, this.headerSize() + _piecesize * index_piece, piece);

      writer.force(true);
      writer_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to write tmp file piece");
      e.printStackTrace();
    }
  }
Ejemplo n.º 3
0
  /**
   * Initialises the Ciphers for both encryption and decryption using the generated or supplied
   * secret key.
   *
   * @param algorithm
   * @param secret
   * @throws Exception
   */
  private void initSymCiphers(String algorithm, SecretKey secret) throws Exception {
    log.debug("initializing symmetric ciphers (pool size=%d)", cipher_pool_size);

    for (int i = 0; i < cipher_pool_size; i++) {
      encoding_ciphers[i] =
          symProvider != null && !symProvider.trim().isEmpty()
              ? Cipher.getInstance(algorithm, symProvider)
              : Cipher.getInstance(algorithm);
      encoding_ciphers[i].init(Cipher.ENCRYPT_MODE, secret);

      decoding_ciphers[i] =
          symProvider != null && !symProvider.trim().isEmpty()
              ? Cipher.getInstance(algorithm, symProvider)
              : Cipher.getInstance(algorithm);
      decoding_ciphers[i].init(Cipher.DECRYPT_MODE, secret);

      encoding_locks[i] = new ReentrantLock();
      decoding_locks[i] = new ReentrantLock();
    }

    // set the version
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.reset();
    digest.update(secret.getEncoded());

    byte[] tmp = digest.digest();
    symVersion = Arrays.copyOf(tmp, tmp.length);
    // symVersion = byteArrayToHexString(digest.digest());
    log.debug("initialized symmetric ciphers with secret key (" + symVersion.length + " bytes)");
  }
Ejemplo n.º 4
0
 /**
  * Chops a token to the specified length and adds dots.
  *
  * @param token token to be chopped
  * @param max maximum length
  * @return chopped token
  */
 public static byte[] chop(final byte[] token, final int max) {
   if (token.length <= max) return token;
   final byte[] tt = Arrays.copyOf(token, max);
   if (max > 2) tt[max - 3] = '.';
   if (max > 1) tt[max - 2] = '.';
   if (max > 0) tt[max - 1] = '.';
   return tt;
 }
Ejemplo n.º 5
0
 /**
  * Normalizes all whitespace occurrences from the specified token.
  *
  * @param token token
  * @return normalized token
  */
 public static byte[] norm(final byte[] token) {
   final int l = token.length;
   final byte[] tmp = new byte[l];
   int c = 0;
   boolean ws1 = true;
   for (final byte t : token) {
     final boolean ws2 = ws(t);
     if (ws2 && ws1) continue;
     tmp[c++] = ws2 ? (byte) ' ' : t;
     ws1 = ws2;
   }
   if (c > 0 && ws(tmp[c - 1])) --c;
   return c == l ? tmp : Arrays.copyOf(tmp, c);
 }
Ejemplo n.º 6
0
 private void setSymVersion(byte[] symVersion) {
   this.symVersion = Arrays.copyOf(symVersion, symVersion.length);
 }