Top Description Inners Fields Constructors Methods
com.ongres.scram.common.util

public Class CryptoUtil

extends Object
Class Inheritance
Imports
javax.crypto.Mac, .SecretKey, .SecretKeyFactory, javax.crypto.spec.PBEKeySpec, .SecretKeySpec, java.security.InvalidKeyException, .SecureRandom, java.security.spec.InvalidKeySpecException

Utility static methods for cryptography related tasks.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

The bytes of the computed Hi value
hi
(SecretKeyFactory
The SecretKeyFactory to generate the SecretKey
secretKeyFactory
,
int
The length of the key (in bits)
keyLength
,
char[]
The char array to compute the Hi function
value
,
byte[]
The salt
salt
,
int
The number of iterations
iterations
)

Compute the "Hi" function for SCRAM.

public static byte[]

Returns:

The bytes of the computed HMAC value
hmac
(SecretKeySpec
A key of the given algorithm
secretKeySpec
,
Mac
A MAC instance of the given algorithm
mac
,
byte[]
The message to compute the HMAC
message
)

Computes the HMAC of a given message.

public static String

Returns:

The String representing the nonce
nonce
(int
The length of the nonce, in characters/bytes
size
,
SecureRandom
The SecureRandom to use
random
)

Generates a random string (called a 'nonce'), composed of ASCII printable characters, except comma (',').

public static String

Returns:

The String representing the nonce
nonce
(int
The length of the nonce, in characters/bytes
size
)

Generates a random string (called a 'nonce'), composed of ASCII printable characters, except comma (',').

public static byte[]
xor(byte[] value1, byte[] value2)

Computes a byte-by-byte xor operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXCLUDED_CHARback to summary
private static final int EXCLUDED_CHAR
MAX_ASCII_PRINTABLE_RANGEback to summary
private static final int MAX_ASCII_PRINTABLE_RANGE
MIN_ASCII_PRINTABLE_RANGEback to summary
private static final int MIN_ASCII_PRINTABLE_RANGE

Constructor Detail

CryptoUtilback to summary
public CryptoUtil()

Method Detail

hiback to summary
public static byte[] hi(SecretKeyFactory secretKeyFactory, int keyLength, char[] value, byte[] salt, int iterations)

Compute the "Hi" function for SCRAM. Hi(str, salt, i): U1 := HMAC(str, salt + INT(1)) U2 := HMAC(str, U1) ... Ui-1 := HMAC(str, Ui-2) Ui := HMAC(str, Ui-1) Hi := U1 XOR U2 XOR ... XOR Ui where "i" is the iteration count, "+" is the string concatenation operator, and INT(g) is a 4-octet encoding of the integer g, most significant octet first. Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the pseudorandom function (PRF) and with dkLen == output length of HMAC() == output length of H().

Parameters
secretKeyFactory:SecretKeyFactory

The SecretKeyFactory to generate the SecretKey

keyLength:int

The length of the key (in bits)

value:char[]

The char array to compute the Hi function

salt:byte[]

The salt

iterations:int

The number of iterations

Returns:byte[]

The bytes of the computed Hi value

hmacback to summary
public static byte[] hmac(SecretKeySpec secretKeySpec, Mac mac, byte[] message)

Computes the HMAC of a given message. HMAC(key, str): Apply the HMAC keyed hash algorithm (defined in [RFC2104]) using the octet string represented by "key" as the key and the octet string "str" as the input string. The size of the result is the hash result size for the hash function in use. For example, it is 20 octets for SHA-1 (see [RFC3174]).

Parameters
secretKeySpec:SecretKeySpec

A key of the given algorithm

mac:Mac

A MAC instance of the given algorithm

message:byte[]

The message to compute the HMAC

Returns:byte[]

The bytes of the computed HMAC value

nonceback to summary
public static String nonce(int size, SecureRandom random)

Generates a random string (called a 'nonce'), composed of ASCII printable characters, except comma (',').

Parameters
size:int

The length of the nonce, in characters/bytes

random:SecureRandom

The SecureRandom to use

Returns:String

The String representing the nonce

nonceback to summary
public static String nonce(int size)

Generates a random string (called a 'nonce'), composed of ASCII printable characters, except comma (','). It uses a default SecureRandom instance.

Parameters
size:int

The length of the nonce, in characters/bytes

Returns:String

The String representing the nonce

xorback to summary
public static byte[] xor(byte[] value1, byte[] value2) throws IllegalArgumentException

Computes a byte-by-byte xor operation. XOR: Apply the exclusive-or operation to combine the octet string on the left of this operator with the octet string on the right of this operator. The length of the output and each of the two inputs will be the same for this use.

com.ongres.scram.common.util back to summary

private Class CryptoUtil.SecureRandomHolder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final SecureRandom

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
private static final SecureRandom INSTANCE

Constructor Detail

SecureRandomHolderback to summary
private SecureRandomHolder()