Skip to content
Iulius edited this page Oct 20, 2015 · 1 revision

Examples

Generate current Token Example

import me.sniggle.security.common.totp.*;

public class SimpleExample {

  public static void main(String[] args) {
    TOTPGenerator totpGenerator = new TOTPGenerator();
    totpGenerator.init();
    byte[] secret = totpGenerator.generateSecretKey();
    String otp = totpGenerator.generateOTP(secret);
    System.out.println("Token: " + otp);
  }

}

Generate QR Code Image

import me.sniggle.security.common.*;
import me.sniggle.security.common.totp.*;
import me.sniggle.security.common.qr.*;

public class GenerateQRCodeImageExample {

  public static void main(String[] args) throws Exception {
    TOTPGenerator totpGenerator = new TOTPGenerator();
    totpGenerator.init();
    byte[] secretKey = totpGenerator.generateSecretKey();
    QRCodeGenerator qrCodeGenerator = new QRCodeGenerator();
    byte[] pngImageData = qrCodeGenerator.generateQRCode(TwoFactorTypes.TOTP, "myservice", "[email protected]", secretKey, Collections.emptyMap());
    try( OutputStream out = new FileOutputStream(Paths.get("/qr-code.png")) ) {
      out.write(pngImageData);
    }
    System.out.println("QR Code image written to: /qr-code.png");
  }

}

Clone this wiki locally