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

org.nutz.lang.encrypt.MsgDigestOutputStream 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.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.nutz.lang.Lang;

public class MsgDigestOutputStream extends FilterOutputStream {
	
	private MessageDigest md;

	public MsgDigestOutputStream(OutputStream out, MessageDigest md) {
		super(out);
		this.md = md;
	}
	
	public MsgDigestOutputStream(OutputStream out, String name) {
		super(out);
		try {
			this.md = MessageDigest.getInstance(name);
		}
		catch (NoSuchAlgorithmException e) {
			throw Lang.wrapThrow(e);
		}
	}

	public void write(byte[] b) throws IOException {
		this.out.write(b);
		md.update(b);
	}
	
	public void write(byte[] b, int off, int len) throws IOException {
		this.out.write(b, off, len);
		md.update(b, off, len);
	}
	
	public void write(int b) throws IOException {
		this.out.write(b);
		md.update((byte)b);
	}
	
	/**
	 * 获取摘要, 同时MessageDigest也被重置了
	 */
	public String digest() {
		return Lang.fixedHexString(md.digest());
	}
	
	public boolean markSupported() {
		return false;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy