daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Nov 9th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package pyaterochka.app.helpers;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Bitmap.Config;
  6. import android.graphics.Point;
  7. import android.support.v4.view.ViewCompat;
  8. import android.view.Display;
  9. import com.google.zxing.BarcodeFormat;
  10. import com.google.zxing.EncodeHintType;
  11. import com.google.zxing.MultiFormatWriter;
  12. import com.google.zxing.common.BitMatrix;
  13. import java.util.EnumMap;
  14. import java.util.Map;
  15. import org.apache.commons.codec.binary.Base32;
  16. import org.jboss.aerogear.security.otp.Totp;
  17. import org.jboss.aerogear.security.otp.api.Clock;
  18.  
  19. public enum TotpPinGenerator {
  20.     INSTANCE;
  21.  
  22.     private static byte[] hexToBytes(String str) {
  23.         int length = str.length();
  24.         byte[] bArr = new byte[(length / 2)];
  25.         for (int i = 0; i < length; i += 2) {
  26.             if (i >= length) {
  27.                 break;
  28.             }
  29.             bArr[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) + Character.digit(str.charAt(i + 1), 16));
  30.         }
  31.         return bArr;
  32.     }
  33.  
  34.     public Bitmap generateBarCode(Activity activity, String str, BarcodeFormat barcodeFormat) {
  35.         Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
  36.         Point point = new Point();
  37.         defaultDisplay.getSize(point);
  38.         return generateBarCode(str, barcodeFormat, point.x, (int) (((float) point.x) / 2.0f));
  39.     }
  40.  
  41.     public Bitmap generateBarCode(String str, BarcodeFormat barcodeFormat, int i, int i2) {
  42.         Map enumMap = new EnumMap(EncodeHintType.class);
  43.         enumMap.put(EncodeHintType.MARGIN, Integer.valueOf(0));
  44.         try {
  45.             BitMatrix encode = new MultiFormatWriter().encode(str, barcodeFormat, i, i2, enumMap);
  46.             int width = encode.getWidth();
  47.             int height = encode.getHeight();
  48.             int[] iArr = new int[(width * height)];
  49.             for (int i3 = 0; i3 < height; i3++) {
  50.                 int i4 = i3 * width;
  51.                 for (int i5 = 0; i5 < width; i5++) {
  52.                     iArr[i4 + i5] = encode.get(i5, i3) ? ViewCompat.MEASURED_STATE_MASK : -1;
  53.                 }
  54.             }
  55.             Bitmap createBitmap = Bitmap.createBitmap(width, height, Config.ARGB_4444);
  56.             createBitmap.setPixels(iArr, 0, width, 0, 0, width, height);
  57.             return createBitmap;
  58.         } catch (Exception e) {
  59.             e.printStackTrace();
  60.             return null;
  61.         }
  62.     }
  63.  
  64.     public Bitmap generateQrCode(Activity activity, String str, BarcodeFormat barcodeFormat) {
  65.         Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
  66.         Point point = new Point();
  67.         defaultDisplay.getSize(point);
  68.         return generateBarCode(str, barcodeFormat, point.y, point.y);
  69.     }
  70.  
  71.     public String generateTotpPin(String str, long j, int i) {
  72.         return new Totp(new Base32().encodeToString(hexToBytes(str)), new Clock(0)).notNow(j / ((long) i));
  73.     }
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top