Created
September 23, 2021 19:57
-
-
Save markscottwright/beb7bcad1814d2b11f3797e12d6cf19c to your computer and use it in GitHub Desktop.
How to extract the totp secret from a QR code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scratch; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import javax.imageio.ImageIO; | |
import com.google.zxing.BinaryBitmap; | |
import com.google.zxing.ChecksumException; | |
import com.google.zxing.FormatException; | |
import com.google.zxing.NotFoundException; | |
import com.google.zxing.Result; | |
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; | |
import com.google.zxing.common.HybridBinarizer; | |
import com.google.zxing.qrcode.QRCodeReader; | |
/** | |
* Needs: | |
* <pre> | |
* {@code | |
* <dependency> | |
* <groupId>com.google.zxing</groupId> | |
* <artifactId>core</artifactId> | |
* <version>3.4.1</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>com.google.zxing</groupId> | |
* <artifactId>javase</artifactId> | |
* <version>3.4.1</version> | |
* </dependency> | |
* } | |
* </pre> | |
*/ | |
public class QrCodeExtractor { | |
public static void main(String[] args) throws IOException, NotFoundException, ChecksumException, FormatException, URISyntaxException { | |
BinaryBitmap image = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new File(args[0]))))); | |
Result result = new QRCodeReader().decode(image); | |
String[] queryParams = new URI(result.getText()).getQuery().split("&"); | |
for (String queryParam : queryParams) { | |
String[] keyAndValue = queryParam.split("=", 2); | |
if(keyAndValue.length > 1 && keyAndValue[0].equals("secret")) | |
System.out.println(keyAndValue[1]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment