signaturegenerator.java

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

JAVA
118
字号
/* 
 * 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.interfaces.*;
import java.security.spec.*;


/**
 * Creates the signature of some data.
 *
 * @author Michael Kaufmann
 */
public class SignatureGenerator
{
	private KeyPair keyPair;
	private Signature signature;
	 
	public static final BigInteger p = new BigInteger(
		"17801190547854226652823756245015999014523215636912" + //$NON-NLS-1$
		"06742732744503144428657887370207706126952521234630" + //$NON-NLS-1$
		"79567156784778466449970650770920727857050009668388" + //$NON-NLS-1$
		"14403412974522117181850604723115003930107995935806" + //$NON-NLS-1$
		"73953487170663198022620197149665241350609459137075" + //$NON-NLS-1$
		"94956514672855690606794135837542707371727429551343" + //$NON-NLS-1$
		"320695239"); //$NON-NLS-1$
	
	public static final BigInteger q = new BigInteger(
		"864205495604807476120572616017955259175325408501"); //$NON-NLS-1$
		
	public static final BigInteger g = new BigInteger(
		"17406820753240209518581198012352343653860449079456" + //$NON-NLS-1$
		"13509784958310405999534884558231478515974089409507" + //$NON-NLS-1$
		"25307797094915759492368300574252438761037084473467" + //$NON-NLS-1$
		"18014887611810308304375498519098347260155049469132" + //$NON-NLS-1$
		"94880833954923138500003616464826446084923040787218" + //$NON-NLS-1$
		"18959999056496097769368017749273708962006689187956" + //$NON-NLS-1$
		"744210730"); //$NON-NLS-1$
		
	public static final DSAParameterSpec dsaParameterSpec = new DSAParameterSpec(p, q, g);
	
	
	/**
	 * Creates a Signature Generator. A public key will be generated.
	 */
	public SignatureGenerator()
	{
		try
		{
			// Schl黶selpaar generieren
		
			KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); //$NON-NLS-1$
			keyPairGenerator.initialize(dsaParameterSpec);
			
			keyPair = keyPairGenerator.generateKeyPair();
		
			signature = Signature.getInstance("SHA1withDSA"); //$NON-NLS-1$
			signature.initSign(keyPair.getPrivate());
		}
		catch (Exception e)
		{
			Logger.severe("SignatureGenerator", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	/**
	 * Creates a signature for <code>data</code>.
	 * 
	 * @param data The byte array containing the data to be signed
	 * @param dataOffset The offset of the data
	 * @param dataLength The length of the data
	 * @param signatureArray The byte array into which the signature is written
	 * @param signatureOffset The offset from which to start writing the signature
	 * @param signatureLength The maximum length of the signature
	 * @return The number of signature bytes written
	 * @throws SignatureException If <code>signatureLength</code> is too short or if the parameters are invalid
	 * 
	 * @see java.security.Signature#sign(byte[],int,int)
	 */
	
	public int sign(byte data[], int dataOffset, int dataLength, byte signatureArray[], int signatureOffset, int signatureLength) throws SignatureException
	{
		signature.update(data, dataOffset, dataLength);
		return signature.sign(signatureArray, signatureOffset, signatureLength);
	}
	
	/**
	 * Returns the public key.
	 */
	public byte[] getPublicKey()
	{
		return ((DSAPublicKey)keyPair.getPublic()).getY().toByteArray();
	} 
}

⌨️ 快捷键说明

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