📄 securityutils.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util;import java.io.*;import java.math.*;import java.security.*;import java.security.cert.*;import java.security.spec.*;import java.util.*;import java.util.zip.*;import javax.crypto.*;import javax.crypto.spec.*;/** * This class contains a large number of static methods for performing * security-related primitives, such as encrypt, decrypt, etc... * * @version $Id: SecurityUtils.java 2514 2005-05-26 22:14:24Z amislove $ * @author amislove */public class SecurityUtils { // ----- STATIC CONFIGURATION FIELDS ----- /** * The name of the asymmetric cipher to use. */ public final static String ASYMMETRIC_ALGORITHM = "RSA/ECB/OAEPPadding"; /** * DESCRIBE THE FIELD */ public final static String DEPRECATED_ASYMMETRIC_ALGORITHM = "RSA"; /** * The name of the symmetric cipher to use. */ public final static String SYMMETRIC_ALGORITHM = "DES"; /** * The name of the asymmetric generator to use. */ public final static String ASYMMETRIC_GENERATOR = "RSA"; /** * The name of the symmetric cipher to use. */ public final static String SYMMETRIC_GENERATOR = "DES"; /** * The name of the signature algorithm to use. */ public final static String SIGNATURE_ALGORITHM = "SHA1withRSA"; /** * The length of the symmetric keys */ public final static int SYMMETRIC_KEY_LENGTH = 56; /** * The name of the hash function. */ public final static String HASH_ALGORITHM = "SHA1"; /** * The name of the hmac function. */ public final static String HMAC_ALGORITHM = "MD5"; /** * The name of the apop function. */ public final static String APOP_ALGORITHM = "MD5"; /** * The length of hmac keys */ public final static int HMAC_KEY_LENGTH = 64; /** * The ipad of hmac keys, as defined in RFC 2195 */ public final static byte HMAC_IPAD_BYTE = 0x36; /** * The opad of hmac keys, as defined in RFC 2195 */ public final static byte HMAC_OPAD_BYTE = 0x5C; /** * The ipad byte array for use in hmac */ public final static byte[] HMAC_IPAD = new byte[HMAC_KEY_LENGTH]; /** * The opad byte array for use in hmac */ public final static byte[] HMAC_OPAD = new byte[HMAC_KEY_LENGTH]; // ----- STATIC CIPHER OBJECTS ----- /** * The message digest used for doing hashing */ private static MessageDigest hash; /** * The message digest used for doing apop */ private static MessageDigest apop; /** * The message digest used for doing hmacing */ private static MessageDigest hmac1; /** * The message digest used for doing hmacing */ private static MessageDigest hmac2; /** * The cipher used to encrypt/decrypt data using DES */ private static Cipher cipherSymmetric; /** * The cipher used to encrypt/decrypt data using RSA */ private static Cipher cipherAsymmetric; private static Cipher deprecatedCipherAsymmetric; /** * The generator used to generate DES keys */ private static KeyGenerator generatorSymmetric; /** * The generator used to generate RSA keys */ private static KeyPairGenerator generatorAsymmetric; /** * The signature used for verification and signing data. */ private static Signature signature; /** * Make the constructor private so no SecurityUtils is ever created. */ private SecurityUtils() { } /** * Utility method for serializing an object to a byte[]. * * @param o The object to serialize * @return The byte[] of the object * @exception IOException If serialization does not happen properly */ public static byte[] serialize(Object o) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new XMLObjectOutputStream(new BufferedOutputStream(new GZIPOutputStream(baos))); oos.writeObject(o); oos.flush(); oos.close(); return baos.toByteArray(); } /** * Utility method for deserializing an object from a byte[] * * @param data The data to deserialize * @return The object * @exception IOException If deserialization does not happen properly * @exception ClassNotFoundException If the deserialized class is not found */ public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException { ByteArrayInputStream bais = new ByteArrayInputStream(data); ObjectInputStream ois = new XMLObjectInputStream(new BufferedInputStream(new GZIPInputStream(bais))); return ois.readObject(); } /** * Utility method for determining the hash of a byte[] using a secure hashing * algorithm. * * @param input The input * @return The hash value * @exception SecurityException If the hashing does not happen properly */ public static byte[] hash(byte[] input) throws SecurityException { synchronized (hash) { return hash.digest(input); } } /** * Utility method for determining the apop of a challenge and password using a * secure hashing algorithm. * * @param password The password * @param challenge The challengr * @return The hash value * @exception SecurityException If the hashing does not happen properly */ public static byte[] apop(byte[] challenge, byte[] password) throws SecurityException { synchronized (apop) { apop.update(challenge); apop.update(password); return apop.digest(); } } /** * Utility method for determining the hmac of a byte[] and key using a secure * hashing algorithm. * * @param text The text * @param key The key * @return The hmac value * @exception SecurityException If the hmacing does not happen properly */ public static byte[] hmac(byte[] key, byte[] text) throws SecurityException { synchronized (hmac1) { byte[] realKey = new byte[HMAC_KEY_LENGTH]; System.arraycopy(key, 0, realKey, 0, (key.length < realKey.length ? key.length : realKey.length));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -