SignatureRSA.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch.jce;
00031 
00032 import java.math.BigInteger;
00033 import java.security.*;
00034 import java.security.spec.*;
00035 
00036 public class SignatureRSA implements com.jcraft.jsch.SignatureRSA{
00037 
00038   java.security.Signature signature;
00039   KeyFactory keyFactory;
00040 
00041   public void init() throws Exception{
00042     signature=java.security.Signature.getInstance("SHA1withRSA");
00043     keyFactory=KeyFactory.getInstance("RSA");
00044   }     
00045   public void setPubKey(byte[] e, byte[] n) throws Exception{
00046     RSAPublicKeySpec rsaPubKeySpec = 
00047     new RSAPublicKeySpec(new BigInteger(n),
00048                  new BigInteger(e));
00049     PublicKey pubKey=keyFactory.generatePublic(rsaPubKeySpec);
00050     signature.initVerify(pubKey);
00051   }
00052   public void setPrvKey(byte[] d, byte[] n) throws Exception{
00053     RSAPrivateKeySpec rsaPrivKeySpec = 
00054     new RSAPrivateKeySpec(new BigInteger(n),
00055                   new BigInteger(d));
00056     PrivateKey prvKey = keyFactory.generatePrivate(rsaPrivKeySpec);
00057     signature.initSign(prvKey);
00058   }
00059   public byte[] sign() throws Exception{
00060     byte[] sig=signature.sign();      
00061     return sig;
00062   }
00063   public void update(byte[] foo) throws Exception{
00064    signature.update(foo);
00065   }
00066   public boolean verify(byte[] sig) throws Exception{
00067     int i=0;
00068     int j=0;
00069     byte[] tmp;
00070 
00071     if(sig[0]==0 && sig[1]==0 && sig[2]==0){
00072     j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
00073     ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
00074     i+=j;
00075     j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
00076     ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
00077     tmp=new byte[j]; 
00078     System.arraycopy(sig, i, tmp, 0, j); sig=tmp;
00079     }
00080 //System.err.println("j="+j+" "+Integer.toHexString(sig[0]&0xff));
00081     return signature.verify(sig);
00082   }
00083 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1