Package p2pradio.packets.security

Source Code of p2pradio.packets.security.SignatureChecker

/*
* 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�r 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);
  }
}
TOP

Related Classes of p2pradio.packets.security.SignatureChecker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.