All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.lang.encrypt.MsgDigestInputStream Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.lang.encrypt;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.nutz.lang.Lang;

public class MsgDigestInputStream extends FilterInputStream {

	private MessageDigest md;

	public MsgDigestInputStream(InputStream in, MessageDigest md) {
		super(in);
		this.md = md;
	}
	
	public MsgDigestInputStream(InputStream in, String name) {
		super(in);
		try {
			this.md = MessageDigest.getInstance(name);
		}
		catch (NoSuchAlgorithmException e) {
			throw Lang.wrapThrow(e);
		}
	}
	
	public int read() throws IOException {
		int b = this.in.read();
		md.update((byte)b);
		return b;
	}
	
	public int read(byte[] b) throws IOException {
		int len = this.in.read(b);
		if (-1 != len)
		    md.update(b, 0 , len);
		return len;
	}
	
	public int read(byte[] b, int off, int len) throws IOException {
		int len2 = this.in.read(b, off, len);
		md.update(b, off, len2);
		return len2;
	}
	
	/**
	 * 获取摘要, 同时MessageDigest也被重置了
	 */
	public String digest() {
		return Lang.fixedHexString(md.digest());
	}
	
	public boolean markSupported() {
		return false;
	}
	
	public synchronized void reset() throws IOException {
		super.reset();
		md.reset();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy