Package io.mangoo.utils
Class TotpUtils
- java.lang.Object
-
- io.mangoo.utils.TotpUtils
-
public class TotpUtils extends Object
- Author:
- svenkubiak
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringcreateSecret()Generates a 64 byte (512 bit) secretstatic StringgetOtpauthURL(String name, String issuer, String secret, HmacShaAlgorithm algorithm, String digits, String period)Generates a otpauth code to share a secret with a userstatic StringgetQRCode(String name, String issuer, String secret, HmacShaAlgorithm algorithm, String digits, String period)Generates a QR code link from google charts API to share a secret with a userstatic StringgetTotp(String secret)Creates the current TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time periodstatic StringgetTotp(String secret, HmacShaAlgorithm algorithm, int digits, int period)Creates the current TOTP based on the given parametersstatic booleanverifiedTotp(String secret, String totp)Verifies a given TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time period
-
-
-
Method Detail
-
createSecret
public static String createSecret()
Generates a 64 byte (512 bit) secret- Returns:
- A 64 characters random string based on SecureRandom
-
getTotp
public static String getTotp(String secret)
Creates the current TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time period- Parameters:
secret- The secret to use- Returns:
- The totp value or null if generation failed
-
getTotp
public static String getTotp(String secret, HmacShaAlgorithm algorithm, int digits, int period)
Creates the current TOTP based on the given parameters- Parameters:
secret- The secret to usealgorithm- The algorithm to usedigits- The digits to use (6 or 8)period- The time period in seconds- Returns:
- The totp value or null if generation failed
-
verifiedTotp
public static boolean verifiedTotp(String secret, String totp)
Verifies a given TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time period- Parameters:
secret- The secret to usetotp- The TOTP to verify- Returns:
- True if the TOTP is valid, false otherwise
-
getQRCode
public static String getQRCode(String name, String issuer, String secret, HmacShaAlgorithm algorithm, String digits, String period)
Generates a QR code link from google charts API to share a secret with a user- Parameters:
name- The name of the accountissuer- The name of the issuersecret- The secret to usealgorithm- The algorithm to usedigits- The number of digits to useperiod- The period to use- Returns:
- An URL to Google charts API with the QR code
-
getOtpauthURL
public static String getOtpauthURL(String name, String issuer, String secret, HmacShaAlgorithm algorithm, String digits, String period)
Generates a otpauth code to share a secret with a user- Parameters:
name- The name of the accountissuer- The name of the issuersecret- The secret to usealgorithm- The algorithm to usedigits- The number of digits to useperiod- The period to use- Returns:
- An otpauth url
-
-