Class OTPUtil

    • 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
    • Constructor Detail

      • OTPUtil

        OTPUtil()
    • 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 - the MessageDigest to use when generating the hash
        input - 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 algorithm
        input - 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 hash
        alternateDictionary - the alternate dictionary to use (if null, 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 hash
        responseType - the response type
        alternateDictionary - the alternate dictionary to use (if null, 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 words
        algorithm - 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 hash
        index - the index of the word to obtain, must be between 0 and 5 (inclusive)
        alternateDictionary - the alternate dictionary to use (if null, 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
      • getPasswordFormatTypeChoiceIndex

        public static int getPasswordFormatTypeChoiceIndex​(String passwordFormatType)
                                                    throws SaslException
        Throws:
        SaslException
      • validateSequenceNumber

        public static void validateSequenceNumber​(int sequenceNumber)
                                           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)