Class HOTP

    • Field Detail

      • passwordLength

        protected final int passwordLength
        Number of digits for generated code in range 6...8, defaults to 6
      • algorithm

        protected final HMACAlgorithm algorithm
        Hashing algorithm used to generate code, defaults to SHA1
      • secret

        protected final byte[] secret
        Secret key used to generate the code, this should be a base32 string
    • Method Detail

      • fromURI

        public static HOTP fromURI​(URI uri)
                            throws URISyntaxException
        Build a TOTPGenerator from an OTPAuth URI
        Parameters:
        uri - OTPAuth URI
        Returns:
        HOTP
        Throws:
        URISyntaxException - when URI cannot be parsed
      • withDefaultValues

        public static HOTP withDefaultValues​(byte[] secret)
        Create a com.bastiaanjansen.otp.HOTPGenerator with default values
        Parameters:
        secret - used to generate hash
        Returns:
        a com.bastiaanjansen.otp.HOTPGenerator with default values
      • getURI

        public URI getURI​(int counter,
                          String issuer)
                   throws URISyntaxException
        Create an OTPAuth URI for easy user on-boarding with only an issuer
        Parameters:
        counter - of URI
        issuer - name for URI
        Returns:
        OTPAuth URI
        Throws:
        URISyntaxException - when URI cannot be created
      • getURI

        public URI getURI​(int counter,
                          String issuer,
                          String account)
                   throws URISyntaxException
        Create an OTPAuth URI for easy user on-boarding with an issuer and account name
        Parameters:
        counter - of URI
        issuer - name for URI
        account - name for URI
        Returns:
        OTPAuth URI
        Throws:
        URISyntaxException - when URI cannot be created
      • getPasswordLength

        public int getPasswordLength()
      • getSecret

        public byte[] getSecret()
      • verify

        public boolean verify​(String code,
                              long counter)
        Checks whether a code is valid for a specific counter with a delay window of 0
        Parameters:
        code - an OTP code
        counter - how many times time interval has passed since 1970
        Returns:
        a boolean, true if code is valid, otherwise false
      • verify

        public boolean verify​(String code,
                              long counter,
                              int delayWindow)
        Checks whether a code is valid for a specific counter taking a delay window into account
        Parameters:
        code - an OTP codee
        counter - how many times time interval has passed since 1970
        delayWindow - window in which a code can still be deemed valid
        Returns:
        a boolean, true if code is valid, otherwise false
      • getURI

        protected URI getURI​(String type,
                             String issuer,
                             String account,
                             Map<String,​String> query)
                      throws URISyntaxException
        Generate an OTPAuth URI
        Parameters:
        type - of OTPAuth URI: totp or hotp
        issuer - name for URI
        account - name for URI
        query - items of URI
        Returns:
        created OTPAuth URI
        Throws:
        URISyntaxException - when URI cannot be created