Ejemplo n.º 1
0
    private static CharSequence trimToLength(CharSequence csq, int maxLen) {
      if (GenericUtils.length(csq) <= maxLen) {
        return csq;
      }

      return csq.subSequence(0, maxLen) + "...";
    }
Ejemplo n.º 2
0
  @Override
  public T getPath(String first, String... more) {
    StringBuilder sb = new StringBuilder();
    if (!GenericUtils.isEmpty(first)) {
      appendDedupSep(sb, first.replace('\\', '/')); // in case we are running on Windows
    }

    if (GenericUtils.length(more) > 0) {
      for (String segment : more) {
        if ((sb.length() > 0) && (sb.charAt(sb.length() - 1) != '/')) {
          sb.append('/');
        }
        // in case we are running on Windows
        appendDedupSep(sb, segment.replace('\\', '/'));
      }
    }

    if ((sb.length() > 1) && (sb.charAt(sb.length() - 1) == '/')) {
      sb.setLength(sb.length() - 1);
    }

    String path = sb.toString();
    String root = null;
    if (path.startsWith("/")) {
      root = "/";
      path = path.substring(1);
    }

    String[] names = GenericUtils.split(path, '/');
    return create(root, names);
  }
 /**
  * @param extraSize Extra size - besides the extension name
  * @return A {@link Buffer} with the extension name set
  */
 protected Buffer getCommandBuffer(int extraSize) {
   String opcode = getName();
   Buffer buffer =
       new ByteArrayBuffer(
           (Integer.SIZE / Byte.SIZE) + GenericUtils.length(opcode) + extraSize + Byte.SIZE);
   buffer.putString(opcode);
   return buffer;
 }
Ejemplo n.º 4
0
  public Collection<KeyPair> loadKeyPairs(
      String resourceKey,
      String pubData,
      String prvData,
      String prvEncryption,
      FilePasswordProvider passwordProvider)
      throws IOException, GeneralSecurityException {
    Decoder b64Decoder = Base64.getDecoder();
    byte[] pubBytes = b64Decoder.decode(pubData);
    byte[] prvBytes = b64Decoder.decode(prvData);
    String password = null;
    if ((GenericUtils.length(prvEncryption) > 0)
        && (!NO_PRIVATE_KEY_ENCRYPTION_VALUE.equalsIgnoreCase(prvEncryption))) {
      password = passwordProvider.getPassword(resourceKey);
    }

    if (GenericUtils.isEmpty(prvEncryption)
        || NO_PRIVATE_KEY_ENCRYPTION_VALUE.equalsIgnoreCase(prvEncryption)
        || GenericUtils.isEmpty(password)) {
      return loadKeyPairs(resourceKey, pubBytes, prvBytes);
    }

    // format is "<cipher><bits>-<mode>" - e.g., "aes256-cbc"
    int pos = prvEncryption.indexOf('-');
    if (pos <= 0) {
      throw new StreamCorruptedException("Missing private key encryption mode in " + prvEncryption);
    }

    String mode = prvEncryption.substring(pos + 1).toUpperCase();
    String algName = null;
    int numBits = 0;
    for (int index = 0; index < pos; index++) {
      char ch = prvEncryption.charAt(index);
      if ((ch >= '0') && (ch <= '9')) {
        algName = prvEncryption.substring(0, index).toUpperCase();
        numBits = Integer.parseInt(prvEncryption.substring(index, pos));
        break;
      }
    }

    if (GenericUtils.isEmpty(algName) || (numBits <= 0)) {
      throw new StreamCorruptedException(
          "Missing private key encryption algorithm details in " + prvEncryption);
    }

    prvBytes =
        PuttyKeyPairResourceParser.decodePrivateKeyBytes(
            prvBytes, algName, numBits, mode, password);
    return loadKeyPairs(resourceKey, pubBytes, prvBytes);
  }