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

📄 jdkdigestsignature.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   JDKDigestSignature.java

package org.bouncycastle.jce.provider;

import java.io.*;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.*;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;

// Referenced classes of package org.bouncycastle.jce.provider:
//			RSAUtil

public class JDKDigestSignature extends Signature
	implements PKCSObjectIdentifiers, X509ObjectIdentifiers
{
	public static class MD2WithRSAEncryption extends JDKDigestSignature
	{

		public MD2WithRSAEncryption()
		{
			super("MD2withRSA", md2, new MD2Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}

	public static class MD5WithRSAEncryption extends JDKDigestSignature
	{

		public MD5WithRSAEncryption()
		{
			super("MD5withRSA", md5, new MD5Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}

	public static class RIPEMD128WithRSAEncryption extends JDKDigestSignature
	{

		public RIPEMD128WithRSAEncryption()
		{
			super("RIPEMD128withRSA", TeleTrusTObjectIdentifiers.ripemd128, new RIPEMD128Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}

	public static class RIPEMD160WithRSAEncryption extends JDKDigestSignature
	{

		public RIPEMD160WithRSAEncryption()
		{
			super("RIPEMD160withRSA", TeleTrusTObjectIdentifiers.ripemd160, new RIPEMD160Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}

	public static class RIPEMD256WithRSAEncryption extends JDKDigestSignature
	{

		public RIPEMD256WithRSAEncryption()
		{
			super("RIPEMD256withRSA", TeleTrusTObjectIdentifiers.ripemd256, new RIPEMD256Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}

	public static class SHA1WithRSAEncryption extends JDKDigestSignature
	{

		public SHA1WithRSAEncryption()
		{
			super("SHA1withRSA", id_SHA1, new SHA1Digest(), new PKCS1Encoding(new RSAEngine()));
		}
	}


	private Digest digest;
	private AsymmetricBlockCipher cipher;
	private AlgorithmIdentifier algId;

	protected JDKDigestSignature(String name, DERObjectIdentifier objId, Digest digest, AsymmetricBlockCipher cipher)
	{
		super(name);
		this.digest = digest;
		this.cipher = cipher;
		algId = new AlgorithmIdentifier(objId, null);
	}

	protected void engineInitVerify(PublicKey publicKey)
		throws InvalidKeyException
	{
		if (!(publicKey instanceof RSAPublicKey))
		{
			throw new InvalidKeyException("Supplied key is not a RSAPublicKey instance");
		} else
		{
			org.bouncycastle.crypto.CipherParameters param = RSAUtil.generatePublicKeyParameter((RSAPublicKey)publicKey);
			digest.reset();
			cipher.init(false, param);
			return;
		}
	}

	protected void engineInitSign(PrivateKey privateKey)
		throws InvalidKeyException
	{
		if (!(privateKey instanceof RSAPrivateKey))
		{
			throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
		} else
		{
			org.bouncycastle.crypto.CipherParameters param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey);
			digest.reset();
			cipher.init(true, param);
			return;
		}
	}

	protected void engineUpdate(byte b)
		throws SignatureException
	{
		digest.update(b);
	}

	protected void engineUpdate(byte b[], int off, int len)
		throws SignatureException
	{
		digest.update(b, off, len);
	}

	protected byte[] engineSign()
		throws SignatureException
	{
		byte hash[];
		hash = new byte[digest.getDigestSize()];
		digest.doFinal(hash, 0);
		byte bytes[] = derEncode(hash);
		return cipher.processBlock(bytes, 0, bytes.length);
		ArrayIndexOutOfBoundsException e;
		e;
		throw new SignatureException("key too small for signature type");
		e;
		throw new SignatureException(e.toString());
	}

	protected boolean engineVerify(byte sigBytes[])
		throws SignatureException
	{
		byte hash[] = new byte[digest.getDigestSize()];
		digest.doFinal(hash, 0);
		DigestInfo digInfo;
		try
		{
			byte sig[] = cipher.processBlock(sigBytes, 0, sigBytes.length);
			digInfo = derDecode(sig);
		}
		catch (Exception e)
		{
			return false;
		}
		if (!digInfo.getAlgorithmId().equals(algId))
			return false;
		byte sigHash[] = digInfo.getDigest();
		if (hash.length != sigHash.length)
			return false;
		for (int i = 0; i < hash.length; i++)
			if (sigHash[i] != hash[i])
				return false;

		return true;
	}

	protected void engineSetParameter(AlgorithmParameterSpec params)
	{
		throw new UnsupportedOperationException("engineSetParameter unsupported");
	}

	/**
	 * @deprecated Method engineSetParameter is deprecated
	 */

	protected void engineSetParameter(String param, Object value)
	{
		throw new UnsupportedOperationException("engineSetParameter unsupported");
	}

	/**
	 * @deprecated Method engineGetParameter is deprecated
	 */

	protected Object engineGetParameter(String param)
	{
		throw new UnsupportedOperationException("engineSetParameter unsupported");
	}

	private byte[] derEncode(byte hash[])
		throws IOException
	{
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		DEROutputStream dOut = new DEROutputStream(bOut);
		DigestInfo dInfo = new DigestInfo(algId, hash);
		dOut.writeObject(dInfo);
		return bOut.toByteArray();
	}

	private DigestInfo derDecode(byte encoding[])
		throws IOException
	{
		ByteArrayInputStream bIn = new ByteArrayInputStream(encoding);
		DERInputStream dIn = new DERInputStream(bIn);
		return new DigestInfo((ASN1Sequence)dIn.readObject());
	}
}

⌨️ 快捷键说明

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