Package org.wildfly.security.sasl.otp
Class OTPUtil
- java.lang.Object
-
- org.wildfly.security.sasl.otp.OTPUtil
-
class OTPUtil extends Object
- Author:
- Farah Juma
-
-
Field Summary
Fields Modifier and Type Field Description static int[]DELIMSprivate static intFOUR_LETTER_WORDS_OFFSETstatic String[]PASSWORD_FORMAT_TYPESprivate static byte[]randomCharDictionarystatic String[]RESPONSE_TYPESprivate static String[]STANDARD_DICTIONARY
-
Constructor Summary
Constructors Constructor Description OTPUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static intcalculateParity(long hash)Break down the given hash into pairs of bits and then calculate the sum of the pairs.static byte[]convertFromHex(String otp)Convert the given hexadecimal OTP value into a hash.static byte[]convertFromWords(String words, String algorithm)Convert the given six words into an OTP hash.static StringconvertToHex(byte[] otp)Convert the given OTP hash into hexadecimal format.static StringconvertToWords(byte[] otp, String[] alternateDictionary)Convert the given OTP hash into a sequence of six words.private static longeightBytesToLong(byte[] b)static StringformatOTP(byte[] otp, String responseType, String[] alternateDictionary)Convert the given OTP hash into the specified format.static StringgenerateRandomAlphanumericString(int length, Random random)static MessageDigestgetMessageDigest(String algorithm)static intgetPasswordFormatTypeChoiceIndex(String passwordFormatType)static intgetResponseTypeChoiceIndex(String responseType)private static StringgetWord(long otp, int index, String[] alternateDictionary)Get the word from the dictionary that corresponds to the given index for the given OTP hash.static byte[]hashAndFold(String algorithm, byte[] input)Pass the given input through a hash function and fold the result to 64 bits.static byte[]hashAndFold(String algorithm, MessageDigest messageDigest, byte[] input)Pass the given input through a hash function and fold the result to 64 bits.private static booleanisDelim(int c, int... delims)private static byte[]longToEightBytes(long value)static StringmessageDigestAlgorithm(String algorithm)private static voidreverse(byte[] bytes, int offset, int length)private static intsearchStandardDictionary(String word)Search the standard OTP dictionary for the given word.static voidskipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi)static voidskipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi, int... delims)static voidvalidateAlgorithm(String algorithm)static voidvalidateAlternateDictionary(String[] dictionary)static voidvalidateAuthorizationId(String authorizationId)static voidvalidatePassPhrase(String passPhrase)static voidvalidateSeed(String seed)static voidvalidateSequenceNumber(int sequenceNumber)static voidvalidateUserName(String userName)
-
-
-
Field Detail
-
DELIMS
public static final int[] DELIMS
-
RESPONSE_TYPES
public static final String[] RESPONSE_TYPES
-
PASSWORD_FORMAT_TYPES
public static final String[] PASSWORD_FORMAT_TYPES
-
FOUR_LETTER_WORDS_OFFSET
private static final int FOUR_LETTER_WORDS_OFFSET
- See Also:
- Constant Field Values
-
randomCharDictionary
private static final byte[] randomCharDictionary
-
STANDARD_DICTIONARY
private static final String[] STANDARD_DICTIONARY
-
-
Method Detail
-
hashAndFold
public static byte[] hashAndFold(String algorithm, MessageDigest messageDigest, byte[] input)
Pass the given input through a hash function and fold the result to 64 bits.- Parameters:
algorithm- the OTP algorithm, must be either "otp-md5" or "otp-sha1"messageDigest- theMessageDigestto use when generating the hashinput- the data to hash- Returns:
- the folded hash
-
hashAndFold
public static byte[] hashAndFold(String algorithm, byte[] input) throws SaslException
Pass the given input through a hash function and fold the result to 64 bits.- Parameters:
algorithm- the OTP algorithminput- the data to hash- Returns:
- the folded hash
- Throws:
SaslException- if the given OTP algorithm is invalid
-
convertToHex
public static String convertToHex(byte[] otp)
Convert the given OTP hash into hexadecimal format.- Parameters:
otp- the OTP hash- Returns:
- the hexadecimal value that corresponds to the given OTP hash
-
convertFromHex
public static byte[] convertFromHex(String otp) throws SaslException
Convert the given hexadecimal OTP value into a hash.- Parameters:
otp- the OTP in hexadecimal format- Returns:
- the OTP hash that corresponds to the given hexadecimal value
- Throws:
SaslException- if an error occurs while parsing the hexadecimal value
-
convertToWords
public static String convertToWords(byte[] otp, String[] alternateDictionary)
Convert the given OTP hash into a sequence of six words.- Parameters:
otp- the OTP hashalternateDictionary- the alternate dictionary to use (ifnull, the standard OTP dictionary will be used)- Returns:
- the sequence of six words that corresponds to the given OTP hash
-
formatOTP
public static String formatOTP(byte[] otp, String responseType, String[] alternateDictionary) throws SaslException
Convert the given OTP hash into the specified format.- Parameters:
otp- the OTP hashresponseType- the response typealternateDictionary- the alternate dictionary to use (ifnull, the standard OTP dictionary will be used)- Returns:
- the formatted OTP
- Throws:
SaslException- if the response type is invalid
-
convertFromWords
public static byte[] convertFromWords(String words, String algorithm) throws SaslException
Convert the given six words into an OTP hash.- Parameters:
words- the OTP formatted as a sequence of six wordsalgorithm- the OTP algorithm- Returns:
- the OTP hash that corresponds to the given sequence of six words
- Throws:
SaslException- if the given algorithm is invalid or if the parity encoded in the last two bits of the final word is incorrect or if an error occurs while parsing the words
-
searchStandardDictionary
private static int searchStandardDictionary(String word)
Search the standard OTP dictionary for the given word.- Parameters:
word- the word to search for- Returns:
- the index of the word if it is found and a value less than 0 otherwise
-
getWord
private static String getWord(long otp, int index, String[] alternateDictionary)
Get the word from the dictionary that corresponds to the given index for the given OTP hash.- Parameters:
otp- the OTP hashindex- the index of the word to obtain, must be between 0 and 5 (inclusive)alternateDictionary- the alternate dictionary to use (ifnull, the standard OTP dictionary will be used)- Returns:
- the word that corresponds to the given index for the given OTP hash
-
calculateParity
private static int calculateParity(long hash)
Break down the given hash into pairs of bits and then calculate the sum of the pairs.- Parameters:
hash- the hash- Returns:
- the two least significant bits of the sum of the pairs of bits from the given hash
-
getResponseTypeChoiceIndex
public static int getResponseTypeChoiceIndex(String responseType) throws SaslException
- Throws:
SaslException
-
getPasswordFormatTypeChoiceIndex
public static int getPasswordFormatTypeChoiceIndex(String passwordFormatType) throws SaslException
- Throws:
SaslException
-
getMessageDigest
public static MessageDigest getMessageDigest(String algorithm) throws NoSuchAlgorithmException
- Throws:
NoSuchAlgorithmException
-
messageDigestAlgorithm
public static String messageDigestAlgorithm(String algorithm) throws NoSuchAlgorithmException
- Throws:
NoSuchAlgorithmException
-
validateAlternateDictionary
public static void validateAlternateDictionary(String[] dictionary) throws SaslException
- Throws:
SaslException
-
validateUserName
public static void validateUserName(String userName) throws SaslException
- Throws:
SaslException
-
validateAuthorizationId
public static void validateAuthorizationId(String authorizationId) throws SaslException
- Throws:
SaslException
-
validateAlgorithm
public static void validateAlgorithm(String algorithm) throws SaslException
- Throws:
SaslException
-
validateSequenceNumber
public static void validateSequenceNumber(int sequenceNumber) throws SaslException- Throws:
SaslException
-
validateSeed
public static void validateSeed(String seed) throws SaslException
- Throws:
SaslException
-
validatePassPhrase
public static void validatePassPhrase(String passPhrase) throws SaslException
- Throws:
SaslException
-
generateRandomAlphanumericString
public static String generateRandomAlphanumericString(int length, Random random)
-
skipDelims
public static void skipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi, int... delims) throws SaslException- Throws:
SaslException
-
skipDelims
public static void skipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi) throws SaslException- Throws:
SaslException
-
isDelim
private static boolean isDelim(int c, int... delims)
-
reverse
private static void reverse(byte[] bytes, int offset, int length)
-
eightBytesToLong
private static long eightBytesToLong(byte[] b)
-
longToEightBytes
private static byte[] longToEightBytes(long value)
-
-