signaturechecker.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 87 行

JAVA
87
字号
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------------
 */

package p2pradio.packets.security;

import p2pradio.logging.Logger;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;


/**
 * Checks the signature of some data.
 *
 * @author Michael Kaufmann
 */
public class SignatureChecker
{
	private PublicKey publicKey;
	private Signature signature;
	
	// F黵 die Klasse SignatureCheckerDummy
	protected SignatureChecker()
	{
	}
	
	/**
	 * Creates a Signature Checker.
	 */
	public SignatureChecker(byte[] publicKeyBytes)
	{
		try
		{
			// Public Key wiederherstellen
			
			KeyFactory keyFactory = KeyFactory.getInstance("DSA"); //$NON-NLS-1$
			publicKey = keyFactory.generatePublic(new DSAPublicKeySpec(new BigInteger(publicKeyBytes), SignatureGenerator.p, SignatureGenerator.q, SignatureGenerator.g));
		
			signature = Signature.getInstance("SHA1withDSA"); //$NON-NLS-1$
			signature.initVerify(publicKey);
		}
		catch (Exception e)
		{
			Logger.severe("SignatureChecker", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	/**
	 * Verifies the signature of <code>data</code>.
	 * 
	 * @param data The byte array containing the data to be verified
	 * @param dataOffset The offset of the data
	 * @param dataLength The length of the data
	 * @param signatureArray The byte array containing the signature
	 * @param signatureOffset The offset of the signature
	 * @param signatureLength The length of the signature
	 * @return <code>true</code> if the signature is valid, <code>false</code> otherwise
	 * @throws SignatureException If the data could not be verified because of invalid parameters
	 * 
	 * @see java.security.Signature#verify(byte[],int,int)
	 */
	public boolean verify(byte data[], int dataOffset, int dataLength, byte signatureArray[], int signatureOffset, int signatureLength) throws SignatureException
	{
		signature.update(data, dataOffset, dataLength);
		return signature.verify(signatureArray, signatureOffset, signatureLength);
	}
}

⌨️ 快捷键说明

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