📄 digesters.java
字号:
/*
Name: Digesters.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.Key;
import java.security.MessageDigest;
import java.security.Security;
/**
* Digest/hash routines for use with BouncyCastle JCE provider
*
* @author Gert Van Ham
* @author hamgert@users.sourceforge.net
* @author http://jcetaglib.sourceforge.net
* @version $Id: Digesters.java,v 1.3 2004/04/15 07:28:25 hamgert Exp $
*/
public class Digesters {
// buffersizes in bytes
private static int BUFFERSIZE_TEXT = 64;
private static int BUFFERSIZE_FILE = 8192;
/**
* Returns a message digest (hash) from a text
*
* @param text text to create message digest from
* @param algorithm hash algorithm (e.g. "Tiger")
* @return message digest in BASE64 format
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
*/
public static StringBuffer hash(StringBuffer text
, String algorithm)
throws CryptoException {
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// hash text
hash(new ByteArrayInputStream(text.toString().getBytes()), dao, algorithm, 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 message digest (hash) from a file
*
* @param file the filename/location
* @param algorithm hash algorithm (e.g. "Tiger")
* @return file digest in BASE64 format
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
*/
public static StringBuffer hashFile(String file
, String algorithm)
throws CryptoException {
FileInputStream fis = null;
ByteArrayOutputStream bao = null;
DataOutputStream dao = null;
try {
fis = new FileInputStream(file);
bao = new ByteArrayOutputStream();
dao = new DataOutputStream(bao);
// hash file
hash(fis, dao, algorithm, 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 message digest (hash) from an inputstream (one-way encryption)
*
* @param is any inputstream to hash
* @param daos digest outputstream
* @param algorithm hash algorithm (e.g. "Tiger")
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
* @throws IOException I/O errors
**/
public static void hash(InputStream is
, DataOutputStream daos
, String algorithm
, int bufferlength)
throws CryptoException, IOException {
try {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
MessageDigest digest = MessageDigest.getInstance(algorithm, "BC");
byte[] buffer = new byte[bufferlength];
int length = 0;
// Read bytes into buffer and feed these bytes into the message
// digest object.
while ((length = is.read(buffer)) != -1) {
digest.update(buffer, 0, length);
}
// Run the algorithm, so it produces the message digest.
byte[] result = digest.digest();
daos.write(result);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
}
}
/**
* Creates a form digest string (= return digest from a text appended to a symmetric key)
*
* @param text StringBuffer form string
* @param digest String the digest/hash algorithm (e.g. MD5)
* @param keyfile String the keystore file(name)
* @param passphrase StringBuffer the passphrase for the keystore
* @param algorithm String encryption algorithm (e.g. "Rijndael")
* @return form digest in BASE64 format
* @throws net.sourceforge.jcetaglib.exceptions.CryptoException encryption errors
* @throws IOException I/O errors
**/
public static StringBuffer formDigest(StringBuffer text
, String digest
, String keyfile
, StringBuffer passphrase
, String algorithm)
throws CryptoException, IOException {
Key key = null;
ByteArrayOutputStream outStr = null;
DataOutputStream dataStr = null;
try {
Security.addProvider(new BouncyCastleProvider());
// read secret key
key = Keystore.loadKey(algorithm, keyfile, passphrase);
// concatenate secret key & form text
outStr = new ByteArrayOutputStream();
dataStr = new DataOutputStream(outStr);
dataStr.write(key.getEncoded());
dataStr.writeBytes(text.toString());
// create hash from the concatenated string
return hash(new StringBuffer(outStr.toString()), digest);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new IOException(ioe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
throw new CryptoException(ex.getMessage());
} finally {
if (dataStr != null) {
// close outputstream
try {
dataStr.close();
} catch (IOException e) {
;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -