Example usage for org.apache.commons.codec.binary Base32 Base32

List of usage examples for org.apache.commons.codec.binary Base32 Base32

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base32 Base32.

Prototype

public Base32() 

Source Link

Usage

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

public int getKey() {
    try {//w ww  . j a  v a  2  s  .  c o  m
        Base32 base32 = new Base32();
        byte datos[] = base32.decode(valor);
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(datos));
        int key = dataInputStream.readInt();

        return key;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.generateSecretKey instead
 * @return/*  w  ww.  j  a v  a  2 s .c o  m*/
 */
@Deprecated
public static synchronized String generateSecretKey() {
    try {
        byte[] buffer = new byte[80 / 8];
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.nextBytes(buffer);
        byte[] secretKey = Arrays.copyOf(buffer, 80 / 8);
        byte[] encodedKey = new Base32().encode(secretKey);
        return new String(encodedKey).toLowerCase();
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:io.stallion.utils.Encrypter.java

private static String doDecryptString(String password, String encryptedBase32) throws Exception {
    encryptedBase32 = StringUtils.strip(encryptedBase32, "=");
    String salt = encryptedBase32.substring(0, 16);
    String ivString = encryptedBase32.substring(16, 48);
    byte[] iv = new byte[0];
    try {/*from w ww .  ja va  2s  .  c o  m*/
        iv = Hex.decodeHex(ivString.toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
    encryptedBase32 = encryptedBase32.substring(48);
    Base32 decoder = new Base32();
    byte[] encrypted = decoder.decode(encryptedBase32.toUpperCase());
    SecretKeySpec skeySpec = makeKeySpec(password, salt);

    /*
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec,
            new IvParameterSpec(iv));
      */
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv));

    byte[] original = cipher.doFinal(encrypted);
    return new String(original, Charset.forName("UTF-8"));

}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java

/**
 *
 * @param data/*from w  w  w  .  jav a  2s .  co m*/
 * @return
 */
public String base32Encode(String data) {
    Base32 b32 = new Base32();
    return b32.encodeAsString(data.getBytes(StandardCharsets.UTF_8));
}

From source file:es.wolfi.app.passman.CredentialDisplay.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
        credential = v.findCredentialByGUID(getArguments().getString(CREDENTIAL));
    }/*from   w  w w.  jav a 2 s .  c  o  m*/

    handler = new Handler();
    otp_refresh = new Runnable() {
        @Override
        public void run() {
            int progress = (int) (System.currentTimeMillis() / 1000) % 30;
            otp_progress.setProgress(progress * 100);

            ObjectAnimator animation = ObjectAnimator.ofInt(otp_progress, "progress", (progress + 1) * 100);
            animation.setDuration(1000);
            animation.setInterpolator(new LinearInterpolator());
            animation.start();

            otp.setText(TOTPHelper.generate(new Base32().decode(credential.getOtp())));
            handler.postDelayed(this, 1000);
        }
    };
}

From source file:co.cask.hydrator.plugin.EncoderTest.java

@Test
public void testBase32Encoder() throws Exception {
    String test = "This is a test for testing base32 encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(
            new Encoder.Config("a:BASE32", OUTPUT.toString()));
    transform.initialize(null);//from ww w  .j a v  a2 s. com

    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4")
            .set("e", "5").build(), emitter);

    Base32 base32 = new Base32();
    byte[] expected = base32.encode(test.getBytes("UTF-8"));
    byte[] actual = emitter.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
}

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

public boolean isValido() {
    try {/* w ww  .j  a  va2 s. c  o m*/
        Base32 base32 = new Base32();
        byte datos[] = base32.decode(valor);
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(datos));
        int key = dataInputStream.readInt();
        int numeroAleatorio = dataInputStream.readInt();
        int crcReal = dataInputStream.readInt();

        CRC crc = new CRC();
        crc.update(key).update(numeroAleatorio);

        if (crcReal == crc.getCRC()) {
            return true;
        } else {
            return false;
        }
    } catch (IOException ex) {
        return false;
    }
}

From source file:com.xk72.cocoafob.LicenseGenerator.java

/**
 * Make and return a license for the given {@link LicenseData}.
 * @param licenseData/*from w w  w .  j  a va2s. c  o  m*/
 * @return
 * @throws LicenseGeneratorException If the generation encounters an error, usually due to invalid input.
 * @throws IllegalStateException If the generator is not setup correctly to make licenses.
 */
public String makeLicense(LicenseData licenseData) throws LicenseGeneratorException, IllegalStateException {
    if (!isCanMakeLicenses()) {
        throw new IllegalStateException(
                "The LicenseGenerator cannot make licenses as it was not configured with a private key");
    }

    final String stringData = licenseData.toLicenseStringData();

    try {
        final Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
        dsa.initSign(privateKey, random);
        dsa.update(stringData.getBytes("UTF-8"));

        final byte[] signed = dsa.sign();

        /* base 32 encode the signature */
        String result = new Base32().encodeAsString(signed);

        /* replace O with 8 and I with 9 */
        result = result.replace("O", "8").replace("I", "9");

        /* remove padding if any. */
        result = result.replace("=", "");

        /* chunk with dashes */
        result = split(result, 5);
        return result;
    } catch (NoSuchAlgorithmException e) {
        throw new LicenseGeneratorException(e);
    } catch (NoSuchProviderException e) {
        throw new LicenseGeneratorException(e);
    } catch (InvalidKeyException e) {
        throw new LicenseGeneratorException(e);
    } catch (SignatureException e) {
        throw new LicenseGeneratorException(e);
    } catch (UnsupportedEncodingException e) {
        throw new LicenseGeneratorException(e);
    }
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java

/**
 *
 * @param b32data/*from ww w .  j a v  a 2s.c  om*/
 * @return
 */
public String base32Decode(String b32data) {
    Base32 b32 = new Base32();
    return new String(b32.decode(b32data), StandardCharsets.UTF_8);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java

/**
 *
 * @param b32data//from w w  w . jav  a2 s  .  c  om
 * @return
 */
public Number getLengthBase32(String b32data) {
    Base32 b32 = new Base32();
    byte[] data = b32.decode(b32data);
    return data.length;
}