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[]
DELIMS
private static int
FOUR_LETTER_WORDS_OFFSET
static String[]
PASSWORD_FORMAT_TYPES
private static byte[]
randomCharDictionary
static String[]
RESPONSE_TYPES
private 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 int
calculateParity(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 String
convertToHex(byte[] otp)
Convert the given OTP hash into hexadecimal format.static String
convertToWords(byte[] otp, String[] alternateDictionary)
Convert the given OTP hash into a sequence of six words.private static long
eightBytesToLong(byte[] b)
static String
formatOTP(byte[] otp, String responseType, String[] alternateDictionary)
Convert the given OTP hash into the specified format.static String
generateRandomAlphanumericString(int length, Random random)
static MessageDigest
getMessageDigest(String algorithm)
static int
getPasswordFormatTypeChoiceIndex(String passwordFormatType)
static int
getResponseTypeChoiceIndex(String responseType)
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.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 boolean
isDelim(int c, int... delims)
private static byte[]
longToEightBytes(long value)
static String
messageDigestAlgorithm(String algorithm)
private static void
reverse(byte[] bytes, int offset, int length)
private static int
searchStandardDictionary(String word)
Search the standard OTP dictionary for the given word.static void
skipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi)
static void
skipDelims(org.wildfly.common.iteration.CodePointIterator di, org.wildfly.common.iteration.CodePointIterator cpi, int... delims)
static void
validateAlgorithm(String algorithm)
static void
validateAlternateDictionary(String[] dictionary)
static void
validateAuthorizationId(String authorizationId)
static void
validatePassPhrase(String passPhrase)
static void
validateSeed(String seed)
static void
validateSequenceNumber(int sequenceNumber)
static void
validateUserName(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
- theMessageDigest
to 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)
-
-