1、 protected final int lineLength; private final int chunkSeparatorLength; protected BaseNCodec(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength) this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, (byte)61); protected BaseNCodec(int unenc
2、odedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength, byte pad) this.unencodedBlockSize = unencodedBlockSize; this.encodedBlockSize = encodedBlockSize; boolean useChunking = (lineLength 0) & (chunkSeparatorLength 0); this.lineLength = (useChunking ? lineLength / encodedBlock
3、Size * encodedBlockSize : this.chunkSeparatorLength = chunkSeparatorLength; this.pad = pad; boolean hasData(Context context) return context.buffer != null; int available(Context context)= null ? context.pos - context.readPos : 0; protected int getDefaultBufferSize() return 8192; private byte resizeB
4、uffer(Context context) if (context.buffer = null) context.buffer = new bytegetDefaultBufferSize(); context.pos = 0; context.readPos = 0; else byte b = new bytecontext.buffer.length * 2; System.arraycopy(context.buffer, 0, b, 0, context.buffer.length); context.buffer = b; return context.buffer; prote
5、cted byte ensureBufferSize(int size, Context context) if (context.buffer = null) | (context.buffer.length = context.pos) context.buffer = null; return len; return context.eof ? -1 : return false; public Object encode(Object obj) throws EncoderException if (!(obj instanceof byte) throw new EncoderExc
6、eption(Parameter supplied to Base-N encode is not a byte); return encode(byte)obj); public String encodeToString(byte pArray) return StringUtils.newStringUtf8(encode(pArray); public String encodeAsString(byte pArray) public Object decode(Object obj) throws DecoderException if (obj instanceof byte) r
7、eturn decode(byte)obj); if (obj instanceof String) return decode(String)obj); throw new DecoderException(Parameter supplied to Base-N decode is not a byte or a String public byte decode(String pArray) return decode(StringUtils.getBytesUtf8(pArray); public byte decode(byte pArray) if (pArray = null)
8、| (pArray.length = 0) return pArray; Context context = new Context(); decode(pArray, 0, pArray.length, context); decode(pArray, 0, -1, context); byte result = new bytecontext.pos; readResults(result, 0, result.length, context); return result; public byte encode(byte pArray) encode(pArray, 0, pArray.
9、length, context); encode(pArray, 0, -1, context); byte buf = new bytecontext.pos - context.readPos; readResults(buf, 0, buf.length, context); return buf; abstract void encode(byte paramArrayOfByte, int paramInt1, int paramInt2, Context paramContext); abstract void decode(byte paramArrayOfByte, int p
10、aramInt1, int paramInt2, Context paramContext); protected abstract boolean isInAlphabet(byte paramByte); public boolean isInAlphabet(byte arrayOctet, boolean allowWSPad) for (int i = 0; i 0) len += (len + this.lineLength - 1L) / this.lineLength * this.chunkSeparatorLength; static class Context int i
11、bitWorkArea; long lbitWorkArea; byte buffer; int pos; int readPos; boolean eof; int currentLinePos; int modulus; public String toString() return String.format(%sbuffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, modulus=%s, pos=%s, readPos=%s, new Object getClass().getSimpleName
12、(), Arrays.toString(this.buffer), Integer.valueOf(this.currentLinePos), Boolean.valueOf(this.eof), Integer.valueOf(this.ibitWorkArea), Long.valueOf(this.lbitWorkArea), Integer.valueOf(this.modulus), Integer.valueOf(this.pos), Integer.valueOf(this.readPos) );package mons.codec.digest;import java.io.I
13、OException;import java.io.InputStream;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import mons.codec.binary.Hex;import mons.codec.binary.StringUtils;public class DigestUtils private static final int STREAM_BUFFER_LENGTH = 1024; private static byte digest(MessageDi
14、gest digest, InputStream data) throws IOException return updateDigest(digest, data).digest(); public static MessageDigest getDigest(String algorithm) try return MessageDigest.getInstance(algorithm); catch (NoSuchAlgorithmException e) throw new IllegalArgumentException(e); public static MessageDigest
15、 getMd2Digest() return getDigest(MD2 public static MessageDigest getMd5Digest()MD5 public static MessageDigest getSha1Digest()SHA-1 public static MessageDigest getSha256Digest()SHA-256 public static MessageDigest getSha384Digest()SHA-384 public static MessageDigest getSha512Digest()SHA-512 public st
16、atic MessageDigest getShaDigest() return getSha1Digest(); public static byte md2(byte data) return getMd2Digest().digest(data); public static byte md2(InputStream data) return digest(getMd2Digest(), data); public static byte md2(String data) return md2(StringUtils.getBytesUtf8(data); public static S
17、tring md2Hex(byte data) return Hex.encodeHexString(md2(data); public static String md2Hex(InputStream data) public static String md2Hex(String data) public static byte md5(byte data) return getMd5Digest().digest(data); public static byte md5(InputStream data) return digest(getMd5Digest(), data); pub
18、lic static byte md5(String data) return md5(StringUtils.getBytesUtf8(data); public static String md5Hex(byte data) return Hex.encodeHexString(md5(data); public static String md5Hex(InputStream data) public static String md5Hex(String data) public static byte sha(byte data) return sha1(data); public
19、static byte sha(InputStream data) public static byte sha(String data) public static byte sha1(byte data) return getSha1Digest().digest(data); public static byte sha1(InputStream data) return digest(getSha1Digest(), data); public static byte sha1(String data) return sha1(StringUtils.getBytesUtf8(data
20、); public static String sha1Hex(byte data) return Hex.encodeHexString(sha1(data); public static String sha1Hex(InputStream data) public static String sha1Hex(String data) public static byte sha256(byte data) return getSha256Digest().digest(data); public static byte sha256(InputStream data) return digest(getSha256Digest(), data); public static byte sha256(String data) return sha256(StringUtils.getBytesUtf8(data); public static String sha256Hex(byte data) return Hex.encodeHexString(sha256(data); public static String sha256Hex(InputStream data)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1