CDigest.java
资源名称:security.rar [点击查看]
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:2k
源码类别:
CA认证
开发平台:
Java
- package org.infosecurity.cryptography;
- import org.infosecurity.cryptography.*;
- /**
- * <p>Title: 摘要算法的抽象类 </p>
- * <p>Description: 摘要算法的抽象类 </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company:bouncycastle org </p>
- */
- public abstract class CDigest
- implements IDigest
- {
- private byte[] xBuf;
- private int xBufOff;
- private long byteCount;
- // 1.构造
- protected CDigest()
- {
- xBuf = new byte[4];
- xBufOff = 0;
- }
- protected CDigest(CDigest t)
- {
- xBuf = new byte[t.xBuf.length];
- System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);
- xBufOff = t.xBufOff;
- byteCount = t.byteCount;
- }
- // 2.输入一个字节的数据
- public void update(
- byte in)
- {
- xBuf[xBufOff++] = in;
- // 填充满一个4个字节了
- if (xBufOff == xBuf.length)
- {
- processWord(xBuf, 0);
- xBufOff = 0;
- }
- byteCount++;
- }
- public void update(
- byte[] in,
- int inOff,
- int len)
- {
- //
- // fill the current word
- //
- while ((xBufOff != 0) && (len > 0))
- {
- update(in[inOff]);
- inOff++;
- len--;
- }
- //
- // process whole words.
- //
- while (len > xBuf.length)
- {
- processWord(in, inOff);
- inOff += xBuf.length;
- len -= xBuf.length;
- byteCount += xBuf.length;
- }
- //
- // load in the remainder.
- //
- while (len > 0)
- {
- update(in[inOff]);
- inOff++;
- len--;
- }
- }
- // 3.完成
- public void finish()
- {
- // 每一个字节是8位
- long bitLength = (byteCount << 3);
- //
- // 附加填充比特一个1,多个0,直到len mod 512 = 448
- //1000,0000B=128
- update((byte)128);
- while (xBufOff != 0)
- {
- update((byte)0);
- }
- // 最后填充8字节(64bit)的长度信息
- processLength(bitLength);
- // 处理最后一块
- processBlock();
- }
- public void reset()
- {
- byteCount = 0;
- xBufOff = 0;
- for ( int i = 0; i < xBuf.length; i++ ) {
- xBuf[i] = 0;
- }
- }
- protected abstract void processWord(byte[] in, int inOff);
- protected abstract void processLength(long bitLength);
- protected abstract void processBlock();
- }