?? signatures.java
字號(hào):
/*
Name: Signatures.java
Licensing: LGPL
API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
Provider: Bouncy Castle (http://www.bouncycastle.org)
Disclaimer:
COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
(C) Copyright 2003 Gert Van Ham
*/
package net.sourceforge.jcetaglib.lib;
import net.sourceforge.jcetaglib.exceptions.CryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import java.io.*;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.Signature;
/**
* Create and verify SIGs (Signatures) with the BouncyCastle JCE provider
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: Signatures.java,v 1.3 2004/04/15 07:28:25 hamgert Exp $
*/
public class Signatures {
// buffersizes in bytes
private static int BUFFERSIZE_TEXT = 64;
private static int BUFFERSIZE_FILE = 8192;
/**
* Generates and returns a SIG (Signature) from a text
*
* @param text text to create SIG from
* @param signingKey the signing key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @return SIG in BASE64 format
* @throws CryptoException for all encryption errors
*/
public static StringBuffer generateSIG(StringBuffer text
, PrivateKey signingKey
, String signame)
throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// Create MAC
generateSIG(new ByteArrayInputStream(text.toString().getBytes()), dao, signingKey, signame, BUFFERSIZE_TEXT);
return new StringBuffer(new String(Base64.encode(bao.toByteArray())));
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Generates and returns a SIG (Signature) from a file
*
* @param file file to create SIG from
* @param signingKey the signing key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @return SIG in BASE64 format
* @throws CryptoException for all encryption errors
*/
public static StringBuffer generateFileSIG(String file
, PrivateKey signingKey
, String signame)
throws CryptoException {
FileInputStream fis = null;
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
fis = new FileInputStream(file);
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// generate mac
generateSIG(fis, dao, signingKey, signame, BUFFERSIZE_FILE);
return new StringBuffer(new String(Base64.encode(bao.toByteArray())));
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (dao != null) {
// close outputstream
try {
dao.close();
} catch (IOException e) {
;
}
}
if (fis != null) {
// close outputstream
try {
fis.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Generates and returns a SIG (Signature) from any inputstream
*
* @param is inputstream to generate SIG from
* @param daos returns SIG code outputstream
* @param signingKey the signing key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @throws IOException I/O errors
* @throws CryptoException for all encryption errors
**/
public static void generateSIG(InputStream is
, DataOutputStream daos
, PrivateKey signingKey
, String signame
, int bufferlength)
throws CryptoException, IOException {
Signature sig = null;
try {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
sig = Signature.getInstance(signame, "BC");
sig.initSign(signingKey);
byte[] buffer = new byte[bufferlength];
int length = 0;
// Read bytes into buffer
while ((length = is.read(buffer)) != -1) {
sig.update(buffer, 0, length);
}
byte[] result = sig.sign();
daos.write(result);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
/**
* Verifies a signature from a text
*
* @param text text to verify
* @param signature the signature (in BASE64 format)
* @param verifyKey the verification key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @return true (verified) or false (invalid)
* @throws CryptoException for encryption errors
*/
public static boolean verifySIG(StringBuffer text
, StringBuffer signature
, PublicKey verifyKey
, String signame)
throws CryptoException {
try {
// Verify SIG
return verifySIG(new ByteArrayInputStream(text.toString().getBytes()), signature, verifyKey, signame, BUFFERSIZE_TEXT);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
}
}
/**
* Verifies a signature from a file
*
* @param file file to verify
* @param signature the signature (in BASE64 format)
* @param verifyKey the verification key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @return true (verified) or false (invalid)
* @throws CryptoException for encryption errors
*/
public static boolean verifyFileSIG(String file
, StringBuffer signature
, PublicKey verifyKey
, String signame)
throws CryptoException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// verify SIG
return verifySIG(fis, signature, verifyKey, signame, BUFFERSIZE_FILE);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new CryptoException(ioe.getMessage());
} finally {
if (fis != null) {
// close outputstream
try {
fis.close();
} catch (IOException e) {
;
}
}
}
}
/**
* Verifies a signature from any inputstream
*
* @param is inputstream to verify
* @param signature the signature (in BASE64 format)
* @param verifyKey the verification key
* @param signame Signature algorithm (e.g. RIPEMD160WithRSA/ISO9796-2)
* @param bufferlength buffer length in bytes
* @return true (verified) or false (invalid)
* @throws IOException I/O errors
* @throws CryptoException for all encryption errors
**/
public static boolean verifySIG(InputStream is
, StringBuffer signature
, PublicKey verifyKey
, String signame
, int bufferlength)
throws CryptoException, IOException {
try {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
Signature sig = Signature.getInstance(signame, "BC");
byte[] sigBytes = Base64.decode(signature.toString());
sig.initVerify(verifyKey);
byte[] buffer = new byte[bufferlength];
int length = 0;
// Read bytes into buffer
while ((length = is.read(buffer)) != -1) {
sig.update(buffer, 0, length);
}
if (!sig.verify(sigBytes)) {
return false;
} else {
return true;
}
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -