Package io.github.pratiyush.totp
Class QRCodeGenerator
java.lang.Object
io.github.pratiyush.totp.QRCodeGenerator
Generates QR codes for TOTP secrets compatible with authenticator apps.
This class creates QR codes containing otpauth:// URIs that can be scanned by Google Authenticator, Microsoft Authenticator, and other compatible apps.
Dependencies
Requires ZXing library (optional dependency). If ZXing is not available,
methods will throw NoClassDefFoundError.
Usage Examples
// Generate QR code as BufferedImage
BufferedImage qr = QRCodeGenerator.generateImage(
secret, "user@example.com", "MyApp", 250);
// Save to file
QRCodeGenerator.saveToFile(secret, "user@example.com", "MyApp",
Path.of("qr.png"), 250);
// Get as Base64 for embedding in HTML
String base64 = QRCodeGenerator.generateBase64(
secret, "user@example.com", "MyApp", 250);
String html = "<img src='data:image/png;base64," + base64 + "'/>";
otpauth URI Format
Generated URIs follow the format:
otpauth://totp/{issuer}:{account}?secret={secret}&issuer={issuer}&algorithm={algo}&digits={digits}&period={period}
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault QR code size in pixelsstatic final intMaximum QR code size in pixelsstatic final intMinimum QR code size in pixels -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildOtpauthUri(String secret, String account, String issuer) Builds an otpauth URI with default configuration.static StringbuildOtpauthUri(String secret, String account, String issuer, TOTPConfig config) Builds an otpauth URI for TOTP.static StringgenerateBase64(String secret, String account, String issuer, int size) Generates a QR code as Base64 encoded PNG.static StringgenerateBase64(String secret, String account, String issuer, int size, TOTPConfig config) Generates a QR code as Base64 encoded PNG with custom configuration.static StringgenerateDataUri(String secret, String account, String issuer, int size) Generates a data URI for embedding in HTML.static BufferedImagegenerateImage(String secret, String account, String issuer, int size) Generates a QR code image.static BufferedImagegenerateImage(String secret, String account, String issuer, int size, TOTPConfig config) Generates a QR code image with custom configuration.static voidsaveToFile(String secret, String account, String issuer, Path path, int size) Saves a QR code to a file.static voidsaveToFile(String secret, String account, String issuer, Path path, int size, TOTPConfig config) Saves a QR code to a file with custom configuration.static voidWrites a QR code to an output stream.
-
Field Details
-
DEFAULT_SIZE
public static final int DEFAULT_SIZEDefault QR code size in pixels- See Also:
-
MIN_SIZE
public static final int MIN_SIZEMinimum QR code size in pixels- See Also:
-
MAX_SIZE
public static final int MAX_SIZEMaximum QR code size in pixels- See Also:
-
-
Method Details
-
buildOtpauthUri
public static String buildOtpauthUri(String secret, String account, String issuer, TOTPConfig config) throws TOTPException Builds an otpauth URI for TOTP.- Parameters:
secret- the Base32 encoded secretaccount- the account name (typically email)issuer- the service nameconfig- optional configuration (null for defaults)- Returns:
- the otpauth URI
- Throws:
TOTPException- if parameters are invalid
-
buildOtpauthUri
public static String buildOtpauthUri(String secret, String account, String issuer) throws TOTPException Builds an otpauth URI with default configuration.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service name- Returns:
- the otpauth URI
- Throws:
TOTPException- if parameters are invalid
-
generateImage
public static BufferedImage generateImage(String secret, String account, String issuer, int size) throws TOTPException Generates a QR code image.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namesize- the image size in pixels- Returns:
- the QR code as BufferedImage
- Throws:
TOTPException- if generation fails
-
generateImage
public static BufferedImage generateImage(String secret, String account, String issuer, int size, TOTPConfig config) throws TOTPException Generates a QR code image with custom configuration.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namesize- the image size in pixelsconfig- optional TOTP configuration- Returns:
- the QR code as BufferedImage
- Throws:
TOTPException- if generation fails
-
generateBase64
public static String generateBase64(String secret, String account, String issuer, int size) throws TOTPException Generates a QR code as Base64 encoded PNG.The result can be used directly in HTML img tags:
<img src="data:image/png;base64,{result}" />- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namesize- the image size in pixels- Returns:
- Base64 encoded PNG
- Throws:
TOTPException- if generation fails
-
generateBase64
public static String generateBase64(String secret, String account, String issuer, int size, TOTPConfig config) throws TOTPException Generates a QR code as Base64 encoded PNG with custom configuration.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namesize- the image size in pixelsconfig- optional TOTP configuration- Returns:
- Base64 encoded PNG
- Throws:
TOTPException- if generation fails
-
generateDataUri
public static String generateDataUri(String secret, String account, String issuer, int size) throws TOTPException Generates a data URI for embedding in HTML.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namesize- the image size in pixels- Returns:
- data URI (data:image/png;base64,...)
- Throws:
TOTPException- if generation fails
-
saveToFile
public static void saveToFile(String secret, String account, String issuer, Path path, int size) throws TOTPException Saves a QR code to a file.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namepath- the file pathsize- the image size in pixels- Throws:
TOTPException- if generation or writing fails
-
saveToFile
public static void saveToFile(String secret, String account, String issuer, Path path, int size, TOTPConfig config) throws TOTPException Saves a QR code to a file with custom configuration.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service namepath- the file pathsize- the image size in pixelsconfig- optional TOTP configuration- Throws:
TOTPException- if generation or writing fails
-
writeTo
public static void writeTo(String secret, String account, String issuer, OutputStream out, String format, int size) throws TOTPException Writes a QR code to an output stream.- Parameters:
secret- the Base32 encoded secretaccount- the account nameissuer- the service nameout- the output streamformat- the image format (PNG, JPEG, etc.)size- the image size in pixels- Throws:
TOTPException- if generation or writing fails
-