private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final Base32 codec = new Base32(); for (final String[] element : BASE32_TEST_CASES) { final byte[] bytes = element[0].getBytes(CHARSET_UTF8); byte[] buffer = ArrayUtils.addAll(bytes, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); assertEquals(element[1], StringUtils.newStringUtf8(codec.encode(buffer, startPasSize, bytes.length))); } }
@Test public void testSingleCharEncoding() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(); final BaseNCodec.Context context = new BaseNCodec.Context(); final byte unencoded[] = new byte[i]; final byte allInOne[] = codec.encode(unencoded); codec = new Base32(); for (int j=0; j< unencoded.length; j++) { codec.encode(unencoded, j, 1, context); } codec.encode(unencoded, 0, -1, context); final byte singly[] = new byte[allInOne.length]; codec.readResults(singly, 0, 100, context); if (!Arrays.equals(allInOne, singly)){ fail(); } } }
/** * Gets S3 key for provided address. * * @param addr Node address. * @return Key. */ private String key(InetSocketAddress addr) { assert addr != null; SB sb = new SB(); if (keyPrefix != null) sb.a(keyPrefix); String addrStr = addr.getAddress().getHostAddress(); if (encryptionSvc != null) { String addrPort = new SB() .a(addrStr) .a(DELIM) .a(addr.getPort()).toString(); byte[] encBytes = encryptionSvc.encrypt(addrPort.getBytes(StandardCharsets.UTF_8)); byte[] base32Bytes = new Base32().encode(encBytes); String encStr = new String(base32Bytes, StandardCharsets.UTF_8).replaceAll("=", ""); sb.a(encStr); } else sb.a(addrStr) .a(DELIM) .a(addr.getPort()); return sb.toString(); }
public static String base32Encode( final byte[] input ) throws IOException { final Base32 base32 = new Base32(); return new String( base32.encode( input ), PwmConstants.DEFAULT_CHARSET ); }
public static String generateSecret() { // Allocating the buffer byte[] buffer = new byte[SECRET_SIZE]; // Filling the buffer with random numbers. rand.nextBytes(buffer); // Getting the key and converting it to Base32 Base32 codec = new Base32(); byte[] secretKey = Arrays.copyOf(buffer, SECRET_SIZE); byte[] encodedKey = codec.encode(secretKey); return new String(encodedKey); }
/** * BASE32 encoding helper (required for OCR representation) * * @param data * binary representation of data to be encoded * @return BASE32 encoded representation of input data */ public static String base32Encode(byte[] data) { Base32 encoder = new Base32(); return new String(encoder.encode(data)).replace("\r\n", ""); }
public Encoder base32() { data = new Base32().encode(data); return this; }
public Encoder base32() { data = new Base32().encode(data); return this; }
/** * Generate the secrete key for mobile devices. * * @return */ public static String calculateSecretKey() throws NoSuchAlgorithmException { byte[] secretKey = new byte[10]; SecureRandom sha1Prng = SecureRandom.getInstance("SHA1PRNG"); sha1Prng.nextBytes(secretKey); Base32 codec = new Base32(); byte[] encodedKey = codec.encode(secretKey); return new String(encodedKey); }
/** * Encode string for Base32. * * @param string String * @param charSet CharSet * @return <code>String</code> Base32 string * @throws UnsupportedEncodingException unsupported encoding exception */ public static String encodeBase32(String string, String charSet) throws UnsupportedEncodingException { if (string == null) { return null; } Base32 base32 = new Base32(); return new String(base32.encode(string.getBytes(charSet))); }
public String getAddressEncoded() throws DecoderException { return new String(new Base32().encode(Hex.decodeHex(address))); }
public JSONObject toJSON() throws JSONException { JSONObject jsonObj = new JSONObject(); jsonObj.put(JSON_SECRET, new String(new Base32().encode(getSecret()))); jsonObj.put(JSON_LABEL, getLabel()); return jsonObj; }
/** * Converts a byte array to a Base32 string. * * @param bytes The input byte array. * @return The output Base32 string. */ public static String getString(final byte[] bytes) { final Base32 codec = new Base32(); final byte[] decodedBytes = codec.encode(bytes); return StringEncoder.getString(decodedBytes); } }
/** * Converts a byte array to a Base32 string. * * @param bytes The input byte array. * @return The output Base32 string. */ public static String getString(final byte[] bytes) { final Base32 codec = new Base32(); final byte[] decodedBytes = codec.encode(bytes); return StringEncoder.getString(decodedBytes); } }
@Override public String getRegistrationId() throws SecurityProviderException { try { //SRS_SecurityClientTpm_25_001: [ This method shall retrieve the EnrollmentKey from the implementation of this abstract class. ] byte[] enrollmentKey = this.getEndorsementKey(); //SRS_SecurityClientTpm_25_002: [ This method shall hash the EnrollmentKey using SHA-256. ] MessageDigest digest = MessageDigest.getInstance(SHA_256); byte[] hash = digest.digest(enrollmentKey); //SRS_SecurityClientTpm_25_003: [ This method shall convert the resultant hash to Base32 to convert all the data to be case agnostic and remove "=" from the string. ] Base32 base32 = new Base32(); byte[] base32Encoded = base32.encode(hash); String registrationId = new String(base32Encoded).toLowerCase(); if (registrationId.contains(EQUALS)) { registrationId = registrationId.replace(EQUALS, "").toLowerCase(); } return registrationId; } catch (NoSuchAlgorithmException e) { //SRS_SecurityClientTpm_25_008: [ This method shall throw SecurityProviderException if any of the underlying API's in generating registration Id. ] throw new SecurityProviderException(e); } }
@Override public String getRegistrationId() throws SecurityProviderException { try { //SRS_SecurityClientTpm_25_001: [ This method shall retrieve the EnrollmentKey from the implementation of this abstract class. ] byte[] enrollmentKey = this.getEndorsementKey(); //SRS_SecurityClientTpm_25_002: [ This method shall hash the EnrollmentKey using SHA-256. ] MessageDigest digest = MessageDigest.getInstance(SHA_256); byte[] hash = digest.digest(enrollmentKey); //SRS_SecurityClientTpm_25_003: [ This method shall convert the resultant hash to Base32 to convert all the data to be case agnostic and remove "=" from the string. ] Base32 base32 = new Base32(); byte[] base32Encoded = base32.encode(hash); String registrationId = new String(base32Encoded).toLowerCase(); if (registrationId.contains(EQUALS)) { registrationId = registrationId.replace(EQUALS, "").toLowerCase(); } return registrationId; } catch (NoSuchAlgorithmException e) { //SRS_SecurityClientTpm_25_008: [ This method shall throw SecurityProviderException if any of the underlying API's in generating registration Id. ] throw new SecurityProviderException(e); } }
/** * Create an Address from a given encoded address. * * @param encodedAddress String * @return {@link Address} */ public static Address createFromEncoded(String encodedAddress) { try { return Address.createFromRawAddress(new String(new Base32().encode(Hex.decodeHex(encodedAddress)))); } catch (DecoderException e) { throw new RuntimeException(e.getCause()); } }
public void testEntry() throws JSONException { byte secret[] = "Das System ist sicher".getBytes(); String label = "5 von 5 Sterne"; String s = "{\"secret\":\""+ new String(new Base32().encode(secret)) +"\",\"label\":\"" + label + "\"}"; Entry e = new Entry(new JSONObject(s)); assertTrue(Arrays.equals(secret, e.getSecret())); assertEquals(label, e.getLabel()); assertEquals(s, e.toJSON()+""); }
public void testEntryURL() throws Exception { try { new Entry("DON'T CARE"); assertTrue(false); } catch (Exception e) { } try { new Entry("https://github.com/0xbb/"); assertTrue(false); } catch (Exception e) { } try { new Entry("otpauth://hotp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"); assertTrue(false); } catch (Exception e){ } try { new Entry("otpauth://totp/ACME"); assertTrue(false); } catch (Exception e){ } Entry entry = new Entry("otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&ALGORITHM=SHA1&digits=6&period=30"); assertEquals("ACME Co - ACME Co:john.doe@email.com", entry.getLabel()); assertEquals("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", new String(new Base32().encode(entry.getSecret()))); }
Intents.init(); String qr = "otpauth://totp/"+code[0] +"?secret="+new String(new Base32().encode(code[1].getBytes())) ;