List of usage examples for org.apache.commons.codec.binary Base32 Base32
public Base32()
From source file:com.torchmind.authenticator.AbstractTokenGenerator.java
/** * {@inheritDoc}/*from w w w . ja v a2s. c o m*/ */ @NonNull @Override public SecretKey parseCode(@NonNull String code) { code = code.replace(" ", "").toUpperCase(); byte[] key = (new Base32()).decode(code); return new SecretKeySpec(key, "Hmac" + this.algorithm.name()); }
From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java
private static String createValor(int key) { try {//from w w w .j a va2 s . c om //Generar el N aleatorio int numeroAleatorio = new Random().nextInt(Integer.MAX_VALUE); CRC crc = new CRC(); crc.update(key).update(numeroAleatorio); //Genera el array de datos con el tipo, key y el n aleatorio ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); dataOutputStream.writeInt(key); dataOutputStream.writeInt(numeroAleatorio); dataOutputStream.writeInt(crc.getCRC()); byte[] datos = byteArrayOutputStream.toByteArray(); //Trasformarlo en un String en Base32 Base32 base32 = new Base32(); String codigoVerificacionSeguro = base32.encodeAsString(datos); //Quitamos el "=" del final pq no se puedo codificar con el cdigo de barras. codigoVerificacionSeguro = codigoVerificacionSeguro.substring(0, codigoVerificacionSeguro.indexOf('=')); return codigoVerificacionSeguro; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java
/** * * @param b32data/*from www. j a va2 s.c om*/ * @return */ public String base32toBase64(String b32data) { //decode base32: Base32 b32 = new Base32(); byte[] data = b32.decode(b32data); //encode base64 return Base64.encodeBase64String(data); }
From source file:co.cask.hydrator.plugin.EncoderTest.java
@Test public void testStringBase32Encoder() throws Exception { String test = "This is a test for testing base32 encoding"; Transform<StructuredRecord, StructuredRecord> transform = new Encoder( new Encoder.Config("a:BASE32", OUTPUTSTR.toString())); transform.initialize(null);/*from w w w .j ava 2 s.co m*/ 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(); String expected = base32.encodeAsString(test.getBytes("UTF-8")); String actual = emitter.getEmitted().get(0).get("a"); Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size()); Assert.assertEquals(expected, actual); }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java
/** * * @param b64data//from w ww .ja v a2 s . c o m * @return */ public String base64toBase32(String b64data) { //decode base64 byte[] data = Base64.decodeBase64(b64data); //encode base32 Base32 b32 = new Base32(); return b32.encodeAsString(data); }
From source file:co.cask.hydrator.plugin.DecoderTest.java
@Test public void testBase32Decoder() throws Exception { String test = "This is a test for testing base32 decoding"; Transform<StructuredRecord, StructuredRecord> encoder = new Encoder( new Encoder.Config("a:BASE32", OUTPUT.toString())); encoder.initialize(null);// ww w .jav a 2s . c om MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>(); encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4") .set("e", "5").build(), emitterEncoded); Base32 base32 = new Base32(); byte[] expected = base32.encode(test.getBytes("UTF-8")); byte[] actual = emitterEncoded.getEmitted().get(0).get("a"); Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size()); Assert.assertArrayEquals(expected, actual); Transform<StructuredRecord, StructuredRecord> decoder = new Decoder( new Decoder.Config("a:BASE32", OUTPUTSTR.toString())); decoder.initialize(null); MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>(); decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded); Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size()); Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a")); }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java
/** * * @param hexData/* w ww . j a v a2s. c o m*/ * @return * @throws DecoderException */ public String hexToBase32(String hexData) throws DecoderException { //decode hex byte[] data = Hex.decodeHex(hexData.toCharArray()); //encode base32 Base32 b32 = new Base32(); return b32.encodeAsString(data); }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java
/** * * @param b32data//from w ww .ja v a2 s . c o m * @return */ public String base32toHex(String b32data) { //decode base32 Base32 b32 = new Base32(); byte[] data = b32.decode(b32data); //encode hex String hexDataString = Hex.encodeHexString(data); return hexDataString; }
From source file:net.cortexx.otp.google.GoogleAuthenticator.java
/** * Creates a URI for a counter-based one-time password consumable by the * <a href="http://code.google.com/p/google-authenticator/">Google Authenticator</a>. * //from w ww . j a v a2 s.c o m * <p> * <b>Note:</b> This class requires the optional <a * href="http://commons.apache.org/proper/commons-codec/"><code>commons-codec</code></a> * dependency. * * @param description * the display name for the token inside the Authenticator * @param algorithm * the algorithm to be used. * @param digits * the number of digits for this password * @param counter * the counter this counter-based password is initialized with * @param secret * the secret for the token. * * @return * the <code>otpauth</code> URI corresponding to the given parameters * * @see <a href="http://code.google.com/p/google-authenticator/wiki/KeyUriFormat">restrictions * applying to the Google Authenticator</a> */ public static String createCounterBasedURI(final String description, final Algorithm algorithm, final int digits, final long counter, final byte... secret) { if (description == null || algorithm == null || secret == null) { throw new NullPointerException(); } if (description.length() == 0) { throw new IllegalArgumentException("'description' must not be empty"); } if (secret.length == 0) { throw new IllegalArgumentException("'secret' must contain at least one byte"); } if (digits < 6 || digits > 8) { throw new IllegalArgumentException("'digits' must be in the range [6..8]"); } if (counter < 0) { throw new IllegalArgumentException("'counter' must be non-negative"); } try { final StringBuilder uri = new StringBuilder().append("otpauth://hotp/") .append(URLEncoder.encode(description, "UTF-8")).append("?secret=") .append(new Base32().encodeToString(secret)).append("&counter=").append(counter); if (algorithm != Algorithm.SHA1) { uri.append("&algorithm=").append(algorithm); } if (digits != 6) { uri.append("&digits=").append(digits); } return uri.toString(); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
From source file:com.xk72.cocoafob.LicenseGenerator.java
/** * Verify the given license for the given {@link LicenseData}. * @param licenseData// w w w.j a v a 2 s . c o m * @param license * @return Whether the license verified successfully. * @throws LicenseGeneratorException If the verification encounters an error, usually due to invalid input. You MUST check the return value of this method if no exception is thrown. * @throws IllegalStateException If the generator is not setup correctly to verify licenses. */ public boolean verifyLicense(LicenseData licenseData, String license) throws LicenseGeneratorException, IllegalStateException { if (!isCanVerifyLicenses()) { throw new IllegalStateException( "The LicenseGenerator cannot verify licenses as it was not configured with a public key"); } final String stringData = licenseData.toLicenseStringData(); /* replace O with 8 and I with 9 */ String licenseSignature = license.replace("8", "O").replace("9", "I"); /* remove dashes */ licenseSignature = licenseSignature.replace("-", ""); /* Pad the output length to a multiple of 8 with '=' characters */ while (licenseSignature.length() % 8 != 0) { licenseSignature += "="; } byte[] decoded = new Base32().decode(licenseSignature); try { Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initVerify(publicKey); dsa.update(stringData.getBytes("UTF-8")); return dsa.verify(decoded); } 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); } }