// Path: ssopay-qywx-common/src/main/java/com/ssopay/qywx/base/Exceptions.java
// public class Exceptions extends RuntimeException {
// 	private static final long serialVersionUID = -6695188947450520803L;
// 	private String key;
// 
// 	private Object[] values;
// 
// 	public Exceptions() {
// 		super();
// 	}
// 
// 	public Exceptions(String message, Throwable throwable) {
// 		super(message, throwable);
// 	}
// 
// 	public Exceptions(String message) {
// 		super(message);
// 	}
// 
// 	public Exceptions(Throwable throwable) {
// 		super(throwable);
// 	}
// 
// 	public Exceptions(String message,String key){
// 		super(message);
// 		this.key = key;
// 	}
// 
// 	public Exceptions(String message,String key,Object value){
// 		super(message);
// 		this.key = key;
// 		this.values = new Object[]{value};
// 	}
// 
// 	public Exceptions(String message,String key,Object[] values){
// 		super(message);
// 		this.key = key;
// 		this.values = values;
// 	}
// 
// 	public String getKey() {
// 		return key;
// 	}
// 
// 	public Object[] getValues() {
// 		return values;
// 	}
// 
// }


import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import org.apache.commons.lang3.Validate;
import com.ssopay.qywx.base.Exceptions;
/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.ssopay.qywx.util;




/**
 * 支持SHA-1/MD5消息摘要的工具类.
 * 
 * 返回ByteSource，可进一步被编码为Hex, Base64或UrlSafeBase64
 * 
 * @author calvin
 */
public class Digests {

	private static final String SHA1 = "SHA-1";
	private static final String MD5 = "MD5";

	private static SecureRandom random = new SecureRandom();

	/**
	 * 对输入字符串进行sha1散列.
	 */
	public static byte[] sha1(byte[] input) {
		return digest(input, SHA1, null, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt) {
		return digest(input, SHA1, salt, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
		return digest(input, SHA1, salt, iterations);
	}

	/**
	 * 对字符串进行散列, 支持md5与sha1算法.
	 */
	private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
		try {
			MessageDigest digest = MessageDigest.getInstance(algorithm);

			if (salt != null) {
				digest.update(salt);
			}

			byte[] result = digest.digest(input);

			for (int i = 1; i < iterations; i++) {
				digest.reset();
				result = digest.digest(result);
			}
			return result;
		} catch (GeneralSecurityException e) {
// Path: ssopay-qywx-common/src/main/java/com/ssopay/qywx/base/Exceptions.java
// public class Exceptions extends RuntimeException {
// 	private static final long serialVersionUID = -6695188947450520803L;
// 	private String key;
// 
// 	private Object[] values;
// 
// 	public Exceptions() {
// 		super();
// 	}
// 
// 	public Exceptions(String message, Throwable throwable) {
// 		super(message, throwable);
// 	}
// 
// 	public Exceptions(String message) {
// 		super(message);
// 	}
// 
// 	public Exceptions(Throwable throwable) {
// 		super(throwable);
// 	}
// 
// 	public Exceptions(String message,String key){
// 		super(message);
// 		this.key = key;
// 	}
// 
// 	public Exceptions(String message,String key,Object value){
// 		super(message);
// 		this.key = key;
// 		this.values = new Object[]{value};
// 	}
// 
// 	public Exceptions(String message,String key,Object[] values){
// 		super(message);
// 		this.key = key;
// 		this.values = values;
// 	}
// 
// 	public String getKey() {
// 		return key;
// 	}
// 
// 	public Object[] getValues() {
// 		return values;
// 	}
// 
// }


// Path: ssopay-qywx-common/src/main/java/com/ssopay/qywx/util/Digests.java
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import org.apache.commons.lang3.Validate;
import com.ssopay.qywx.base.Exceptions;

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.ssopay.qywx.util;




/**
 * 支持SHA-1/MD5消息摘要的工具类.
 * 
 * 返回ByteSource，可进一步被编码为Hex, Base64或UrlSafeBase64
 * 
 * @author calvin
 */
public class Digests {

	private static final String SHA1 = "SHA-1";
	private static final String MD5 = "MD5";

	private static SecureRandom random = new SecureRandom();

	/**
	 * 对输入字符串进行sha1散列.
	 */
	public static byte[] sha1(byte[] input) {
		return digest(input, SHA1, null, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt) {
		return digest(input, SHA1, salt, 1);
	}

	public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
		return digest(input, SHA1, salt, iterations);
	}

	/**
	 * 对字符串进行散列, 支持md5与sha1算法.
	 */
	private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
		try {
			MessageDigest digest = MessageDigest.getInstance(algorithm);

			if (salt != null) {
				digest.update(salt);
			}

			byte[] result = digest.digest(input);

			for (int i = 1; i < iterations; i++) {
				digest.reset();
				result = digest.digest(result);
			}
			return result;
		} catch (GeneralSecurityException e) {
			throw new Exceptions("未经检查的异常。");