首页

关于ftpserver-core源码包中EncryptUtils加密工具类分别进行MD5、SHA、加盐等转码加密处理

标签:EncryptUtils,ftpserver-core,加密工具类,md5,sha,加盐转码     发布时间:2018-05-08   

一、前言

关于apacheftpserver的核心源码包ftpserver-core中org.apache.ftpserver.util.EncryptUtils加密工具类,进行常用加盐转换encrypt处理、MD5加密encryptMD5、基于SHA加密encryptSHA等通用操作。

二、代码示例

1.EncryptUtils类

package org.apache.ftpserver.util;@b@@b@import java.security.MessageDigest;@b@import java.security.NoSuchAlgorithmException;@b@@b@public class EncryptUtils@b@{@b@  public static final byte[] encrypt(byte[] source, String algorithm)@b@    throws NoSuchAlgorithmException@b@  {@b@    MessageDigest md = MessageDigest.getInstance(algorithm);@b@    md.reset();@b@    md.update(source);@b@    return md.digest();@b@  }@b@@b@  public static final String encrypt(String source, String algorithm)@b@    throws NoSuchAlgorithmException@b@  {@b@    byte[] resByteArray = encrypt(source.getBytes(), algorithm);@b@    return StringUtils.toHexString(resByteArray);@b@  }@b@@b@  public static final String encryptMD5(String source)@b@  {@b@    if (source == null) {@b@      source = "";@b@    }@b@@b@    String result = "";@b@    try {@b@      result = encrypt(source, "MD5");@b@    }@b@    catch (NoSuchAlgorithmException ex) {@b@      throw new RuntimeException(ex);@b@    }@b@    return result;@b@  }@b@@b@  public static final String encryptSHA(String source)@b@  {@b@    if (source == null) {@b@      source = "";@b@    }@b@@b@    String result = "";@b@    try {@b@      result = encrypt(source, "SHA");@b@    }@b@    catch (NoSuchAlgorithmException ex) {@b@      throw new RuntimeException(ex);@b@    }@b@    return result;@b@  }@b@}

2.StringUtils类

package org.apache.ftpserver.util;@b@@b@import java.util.Map;@b@@b@public class StringUtils@b@{@b@  public static final String replaceString(String source, String oldStr, String newStr)@b@  {@b@    StringBuilder sb = new StringBuilder(source.length());@b@    int sind = 0;@b@    int cind = 0;@b@    while ((cind = source.indexOf(oldStr, sind)) != -1) {@b@      sb.append(source.substring(sind, cind));@b@      sb.append(newStr);@b@      sind = cind + oldStr.length();@b@    }@b@    sb.append(source.substring(sind));@b@    return sb.toString();@b@  }@b@@b@  public static final String replaceString(String source, Object[] args)@b@  {@b@    int startIndex = 0;@b@    int openIndex = source.indexOf(123, startIndex);@b@    if (openIndex == -1) {@b@      return source;@b@    }@b@@b@    int closeIndex = source.indexOf(125, startIndex);@b@    if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@      return source;@b@    }@b@@b@    StringBuilder sb = new StringBuilder();@b@    sb.append(source.substring(startIndex, openIndex));@b@    while (true) {@b@      String intStr = source.substring(openIndex + 1, closeIndex);@b@      int index = Integer.parseInt(intStr);@b@      sb.append(args[index]);@b@@b@      startIndex = closeIndex + 1;@b@      openIndex = source.indexOf(123, startIndex);@b@      if (openIndex == -1) {@b@        sb.append(source.substring(startIndex));@b@        break;@b@      }@b@@b@      closeIndex = source.indexOf(125, startIndex);@b@      if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@        sb.append(source.substring(startIndex));@b@        break;@b@      }@b@      sb.append(source.substring(startIndex, openIndex));@b@    }@b@    return sb.toString();@b@  }@b@@b@  public static final String replaceString(String source, Map<String, Object> args)@b@  {@b@    int startIndex = 0;@b@    int openIndex = source.indexOf(123, startIndex);@b@    if (openIndex == -1) {@b@      return source;@b@    }@b@@b@    int closeIndex = source.indexOf(125, startIndex);@b@    if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@      return source;@b@    }@b@@b@    StringBuilder sb = new StringBuilder();@b@    sb.append(source.substring(startIndex, openIndex));@b@    while (true) {@b@      String key = source.substring(openIndex + 1, closeIndex);@b@      Object val = args.get(key);@b@      if (val != null) {@b@        sb.append(val);@b@      }@b@@b@      startIndex = closeIndex + 1;@b@      openIndex = source.indexOf(123, startIndex);@b@      if (openIndex == -1) {@b@        sb.append(source.substring(startIndex));@b@        break;@b@      }@b@@b@      closeIndex = source.indexOf(125, startIndex);@b@      if ((closeIndex == -1) || (openIndex > closeIndex)) {@b@        sb.append(source.substring(startIndex));@b@        break;@b@      }@b@      sb.append(source.substring(startIndex, openIndex));@b@    }@b@    return sb.toString();@b@  }@b@@b@  public static final String formatHtml(String source, boolean bReplaceNl, boolean bReplaceTag, boolean bReplaceQuote)@b@  {@b@    StringBuilder sb = new StringBuilder();@b@    int len = source.length();@b@    for (int i = 0; i < len; ++i) {@b@      char c = source.charAt(i);@b@      switch (c)@b@      {@b@      case '"':@b@        if (bReplaceQuote)@b@          sb.append("&quot;");@b@        else@b@          sb.append(c);@b@        break;@b@      case '<':@b@        if (bReplaceTag)@b@          sb.append("&lt;");@b@        else@b@          sb.append(c);@b@        break;@b@      case '>':@b@        if (bReplaceTag)@b@          sb.append("&gt;");@b@        else@b@          sb.append(c);@b@        break;@b@      case '\n':@b@        if (bReplaceNl)@b@          if (bReplaceTag)@b@            sb.append("&lt;br&gt;");@b@          else@b@            sb.append("<br>");@b@        else@b@          sb.append(c);@b@@b@        break;@b@      case '\r':@b@        break;@b@      case '&':@b@        sb.append("&amp;");@b@        break;@b@      default:@b@        sb.append(c);@b@      }@b@    }@b@@b@    return sb.toString();@b@  }@b@@b@  public static final String pad(String src, char padChar, boolean rightPad, int totalLength)@b@  {@b@    int srcLength = src.length();@b@    if (srcLength >= totalLength) {@b@      return src;@b@    }@b@@b@    int padLength = totalLength - srcLength;@b@    StringBuilder sb = new StringBuilder(padLength);@b@    for (int i = 0; i < padLength; ++i) {@b@      sb.append(padChar);@b@    }@b@@b@    if (rightPad)@b@      return src + sb.toString();@b@@b@    return sb.toString() + src;@b@  }@b@@b@  public static final String toHexString(byte[] res)@b@  {@b@    StringBuilder sb = new StringBuilder(res.length << 1);@b@    for (int i = 0; i < res.length; ++i) {@b@      String digit = Integer.toHexString(0xFF & res[i]);@b@      if (digit.length() == 1)@b@        sb.append('0');@b@@b@      sb.append(digit);@b@    }@b@    return sb.toString().toUpperCase();@b@  }@b@@b@  public static final byte[] toByteArray(String hexString)@b@  {@b@    int arrLength = hexString.length() >> 1;@b@    byte[] buff = new byte[arrLength];@b@    for (int i = 0; i < arrLength; ++i) {@b@      int index = i << 1;@b@      String digit = hexString.substring(index, index + 2);@b@      buff[i] = (byte)Integer.parseInt(digit, 16);@b@    }@b@    return buff;@b@  }@b@}