Exemplo n.º 1
0
  public static String maskString(
      String toMasking, char maskChr, StringMaskOption maskOpt, int maskPercent) {

    if (toMasking == null) {
      return null;
    }

    if (maskPercent <= 0) {
      return toMasking;
    }

    StringBuilder sb = new StringBuilder(toMasking.length());

    if (maskPercent >= 100) {
      for (int i = 0; i < toMasking.length(); i++) {
        sb.append(maskChr);
      }

      return sb.toString();
    }

    final int maskLen = (toMasking.length() * maskPercent) / 100;
    int maskCountdown = maskLen;
    int maskStart = 0;

    switch (maskOpt) {
      case MASK_HEAD:
        {
          maskStart = 0;
          break;
        }
      case MASK_END:
        {
          maskStart = toMasking.length() - maskLen;
          break;
        }
      case MASK_MIDDLE:
        {
          maskStart = (toMasking.length() / 2) - maskLen / 2;
          break;
        }
    }

    for (int i = 0; i < toMasking.length(); i++) {

      if (i >= maskStart && i <= maskStart + maskLen) {
        while (maskCountdown-- != 0) {
          sb.append(maskChr);
        }

        i += maskLen;
        continue;
      }

      sb.append(toMasking.charAt(i));
    }

    return sb.toString();
  }
 public static String getHex(byte[] raw) {
   if (raw == null) return null;
   final StringBuilder hex = new StringBuilder(2 * raw.length);
   for (final byte b : raw)
     hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
   return hex.toString();
 }