⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.maverick.crypto.digests;

import com.maverick.crypto.io.ByteArrayWriter;
import java.math.BigInteger;


/**
 * <p>Useful utility class that provides a wrapper around a digest with
 * methods to update the digest with many of the common parameter types
 * used throughout the API.</p>
 * @author Lee David Painter
 * @version $Revision: 1.2 $
 */
public class Hash {
  private Digest digest;

  /**
   * Create a hash with the digest provided.
   * @param digest
   */
  public Hash(String type) {
    this.digest = DigestFactory.createDigest(type);
  }

  public Hash(Digest digest) {
      this.digest = digest;
  }

  /**
   * Update the digest with a BigInteger value. This puts both the integer
   * length of the BigInteger data and the binary data.
   * @param bi
   */
  public void putBigInteger(BigInteger bi) {
    byte[] data = bi.toByteArray();

    putInt(data.length);
    putBytes(data);
  }

  /**
   * Put a single byte into the digest.
   * @param b
   */
  public void putByte(byte b) {
    digest.update(b);
  }

  /**
   * Put a byte array into the digest.
   * @param data
   */
  public void putBytes(byte[] data) {
    digest.update(data, 0, data.length);
  }

  /**
   * Put a byte array into the digest
   * @param data
   * @param offset
   * @param len
   */
  public void putBytes(byte[] data, int offset, int len) {
    digest.update(data, offset, len);
  }

  /**
   * Put an integer into the digest.
   * @param i
   */
  public void putInt(int i) {
    putBytes(ByteArrayWriter.encodeInt(i));
  }

  /**
   * Put a String into the digest.
   * @param str
   */
  public void putString(String str) {
    putInt(str.length());
    putBytes(str.getBytes());
  }

  /**
   * Reset the underlying digest.
   */
  public void reset() {
    digest.reset();
  }

  /**
   * Do the final processing and return the hash.
   * @return the hash
   */
  public byte[] doFinal() {
    byte[] hash = new byte[digest.getDigestSize()];
    digest.doFinal(hash, 0);
    return hash;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -