?? macs.java
字號(hào):
/*
Name: Macs.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 javax.crypto.Mac;
import java.io.*;
import java.security.Key;
import java.security.Security;
/**
* Create MACs (Message Authentication Code) with the BouncyCastle JCE provider
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: Macs.java,v 1.3 2004/04/15 07:28:25 hamgert Exp $
*/
public class Macs {
// buffersizes in bytes
private static int BUFFERSIZE_TEXT = 64;
private static int BUFFERSIZE_FILE = 8192;
/**
* Returns a MAC (Message Authentication Code) from a text
*
* @param text text to create MAC from
* @param keyfile keyfile(name)
* @param passphrase the passphrase for the keystore
* @param algorithm encryption algorithm (e.g. "Rijndael")
* @param macname MAC algorithm (e.g. IDEAMac)
* @return the MAC in BASE64 format
* @throws CryptoException for all encryption errors
*/
public static StringBuffer generateMAC(StringBuffer text
, String keyfile
, StringBuffer passphrase
, String algorithm
, String macname)
throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// Create MAC
generateMAC(new ByteArrayInputStream(text.toString().getBytes()), dao, keyfile, passphrase, algorithm, macname, 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) {
;
}
}
}
}
/**
* Returns a MAC (Message Authentication Code) from a file
*
* @param file file to create MAC from
* @param keyfile keyfile(name)
* @param passphrase the passphrase for the keystore
* @param algorithm encryption algorithm (e.g. "Rijndael")
* @param macname MAC algorithm (e.g. IDEAMac)
* @return the MAC in BASE64 format
* @throws CryptoException for encryption errors
*/
public static StringBuffer generateFileMAC(String file
, String keyfile
, StringBuffer passphrase
, String algorithm
, String macname)
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
generateMAC(fis, dao, keyfile, passphrase, algorithm, macname, 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) {
;
}
}
}
}
/**
* Returns a MAC (Message Authentication Code) from any inputstream
*
* @param is any inputstream to generate HMAC from
* @param daos returns MAC code string outputstream
* @param keyfile keyfile(name)
* @param passphrase the passphrase for the keystore
* @param algorithm encryption algorithm (e.g. "Rijndael")
* @param macname MAC algorithm (e.g. IDEAMac)
* @throws IOException I/O errors
* @throws CryptoException for all encryption errors
**/
public static void generateMAC(InputStream is
, DataOutputStream daos
, String keyfile
, StringBuffer passphrase
, String algorithm
, String macname
, int bufferlength)
throws CryptoException, IOException {
Key secretKey = null;
Mac mac = null;
try {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
// read secret key
secretKey = Keystore.loadKey(algorithm, keyfile, passphrase);
mac = Mac.getInstance(macname, "BC");
mac.init(secretKey);
byte[] buffer = new byte[bufferlength];
int length = 0;
// Read bytes into buffer
while ((length = is.read(buffer)) != -1) {
mac.update(buffer, 0, length);
}
byte[] result = mac.doFinal();
daos.write(result);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -