org.apache.shindig.common.crypto
Class Crypto

java.lang.Object
  extended by org.apache.shindig.common.crypto.Crypto

public final class Crypto
extends Object

Cryptographic utility functions.


Field Summary
static int CIPHER_KEY_LEN
          Use keys of this length for encryption operations
static int HMAC_SHA1_LEN
          Length of HMAC SHA1 output
static SecureRandom RAND
          Use this random number generator instead of creating your own.
 
Method Summary
static byte[] aes128cbcDecrypt(byte[] key, byte[] cipherText)
          AES-128-CBC decryption.
static byte[] aes128cbcDecryptWithIv(byte[] key, byte[] iv, byte[] cipherText, int offset)
          AES-128-CBC decryption with a particular IV.
static byte[] aes128cbcEncrypt(byte[] key, byte[] plain)
          AES-128-CBC encryption.
static byte[] aes128cbcEncryptWithIV(byte[] key, byte[] iv, byte[] plain)
          AES-128-CBC encryption with a given IV.
static byte[] concat(byte[] a, byte[] b)
          Concatenate two byte arrays.
static byte[] getRandomBytes(int numBytes)
          Returns strong random bytes.
static String getRandomDigits(int len)
           
static String getRandomString(int numBytes)
          Gets a hex encoded random string.
static byte[] hmacSha1(byte[] key, byte[] in)
          HMAC sha1
static void hmacSha1Verify(byte[] key, byte[] in, byte[] expected)
          Verifies an HMAC SHA1 hash.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RAND

public static final SecureRandom RAND
Use this random number generator instead of creating your own. This is thread-safe.


CIPHER_KEY_LEN

public static final int CIPHER_KEY_LEN
Use keys of this length for encryption operations

See Also:
Constant Field Values

HMAC_SHA1_LEN

public static final int HMAC_SHA1_LEN
Length of HMAC SHA1 output

See Also:
Constant Field Values
Method Detail

getRandomString

public static String getRandomString(int numBytes)
Gets a hex encoded random string.

Parameters:
numBytes - number of bytes of randomness.

getRandomDigits

public static String getRandomDigits(int len)
Returns:
a random string of digits of the specified length.

getRandomBytes

public static byte[] getRandomBytes(int numBytes)
Returns strong random bytes.

Parameters:
numBytes - number of bytes of randomness

hmacSha1

public static byte[] hmacSha1(byte[] key,
                              byte[] in)
                       throws GeneralSecurityException
HMAC sha1

Parameters:
key - the key must be at least 8 bytes in length.
in - byte array to HMAC.
Returns:
the hash
Throws:
GeneralSecurityException

hmacSha1Verify

public static void hmacSha1Verify(byte[] key,
                                  byte[] in,
                                  byte[] expected)
                           throws GeneralSecurityException
Verifies an HMAC SHA1 hash. Throws if the verification fails.

Parameters:
key -
in -
expected -
Throws:
GeneralSecurityException

aes128cbcEncrypt

public static byte[] aes128cbcEncrypt(byte[] key,
                                      byte[] plain)
                               throws GeneralSecurityException
AES-128-CBC encryption. The IV is returned as the first 16 bytes of the cipher text.

Parameters:
key -
plain -
Returns:
the IV and cipher text
Throws:
GeneralSecurityException

aes128cbcEncryptWithIV

public static byte[] aes128cbcEncryptWithIV(byte[] key,
                                            byte[] iv,
                                            byte[] plain)
                                     throws GeneralSecurityException
AES-128-CBC encryption with a given IV.

Parameters:
key -
iv -
plain -
Returns:
the cipher text
Throws:
GeneralSecurityException

aes128cbcDecrypt

public static byte[] aes128cbcDecrypt(byte[] key,
                                      byte[] cipherText)
                               throws GeneralSecurityException
AES-128-CBC decryption. The IV is assumed to be the first 16 bytes of the cipher text.

Parameters:
key -
cipherText -
Returns:
the plain text
Throws:
GeneralSecurityException

aes128cbcDecryptWithIv

public static byte[] aes128cbcDecryptWithIv(byte[] key,
                                            byte[] iv,
                                            byte[] cipherText,
                                            int offset)
                                     throws GeneralSecurityException
AES-128-CBC decryption with a particular IV.

Parameters:
key - decryption key
iv - initial vector for decryption
cipherText - cipher text to decrypt
offset - offset into cipher text to begin decryption
Returns:
the plain text
Throws:
GeneralSecurityException

concat

public static byte[] concat(byte[] a,
                            byte[] b)
Concatenate two byte arrays.



Copyright © 2007-2012. All Rights Reserved.