public String toIdentityString() { StringBuilder refString = new StringBuilder(); refString.append(IDENTITY_VERSION_1); refString.append(IDENTITY_SEPARATOR); refString.append(Base64.encode(accountUuid)); refString.append(IDENTITY_SEPARATOR); refString.append(Base64.encode(folderServerId)); refString.append(IDENTITY_SEPARATOR); refString.append(Base64.encode(uid)); if (flag != null) { refString.append(IDENTITY_SEPARATOR); refString.append(flag.name()); } return refString.toString(); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * @param binaryData * binary data to encode * @return Base64 characters */ public static byte[] encodeBase64(byte[] binaryData) { return encodeBase64(binaryData, false); }
/** * Closes this output stream, flushing any remaining bytes that must be encoded. The * underlying stream is flushed but not closed. */ @Override public void close() throws IOException { // Notify encoder of EOF (-1). if (doEncode) { base64.encode(singleByte, 0, -1); } else { base64.decode(singleByte, 0, -1); } flush(); }
/** * Decodes Base64 data into octets * * @param base64Data Byte array containing Base64 data * @return Array containing decoded data. */ public static byte[] decodeBase64(byte[] base64Data) { if (base64Data == null || base64Data.length == 0) { return base64Data; } Base64 b64 = new Base64(); long len = (base64Data.length * 3) / 4; byte[] buf = new byte[(int) len]; b64.setInitialBuffer(buf, 0, buf.length); b64.decode(base64Data, 0, base64Data.length); b64.decode(base64Data, 0, -1); // Notify decoder of EOF. // We have no idea what the line-length was, so we // cannot know how much of our array wasn't used. byte[] result = new byte[b64.pos]; b64.readResults(result, 0, result.length); return result; }
/** * Encode to a byte64-encoded integer according to crypto * standards such as W3C's XML-Signature * * @param bigInt a BigInteger * @return A byte array containing base64 character data * @throws NullPointerException if null is passed in */ public static byte[] encodeInteger(BigInteger bigInt) { if (bigInt == null) { throw new NullPointerException("encodeInteger called with null parameter"); } return encodeBase64(toIntegerBytes(bigInt), false); }
return binaryData; Base64 b64 = isChunked ? new Base64() : new Base64(0); b64.setInitialBuffer(buf, 0, buf.length); b64.encode(binaryData, 0, binaryData.length); b64.encode(binaryData, 0, -1); // Notify encoder of EOF. b64.readResults(buf, 0, buf.length);
/** * Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the * Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[]. * * @param pObject * Object to decode * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied. * @throws DecoderException * if the parameter supplied is not of type byte[] */ public Object decode(Object pObject) throws DecoderException { if (!(pObject instanceof byte[])) { throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]"); } return decode((byte[]) pObject); }
byte[] nonce = Base64.decodeBase64(b64Nonce); return Base64.encodeBase64(plainCRAM.getBytes());
/** * Flushes this output stream and forces any buffered output bytes * to be written out to the stream. If propagate is true, the wrapped * stream will also be flushed. * * @param propagate boolean flag to indicate whether the wrapped * OutputStream should also be flushed. * @throws IOException if an I/O error occurs. */ private void flush(boolean propagate) throws IOException { int avail = base64.avail(); if (avail > 0) { byte[] buf = new byte[avail]; int c = base64.readResults(buf, 0, avail); if (c > 0) { out.write(buf, 0, c); } } if (propagate) { out.flush(); } }
/** * Creates a Base64OutputStream such that all data written is either * Base64-encoded or Base64-decoded to the original provided OutputStream. * * @param out OutputStream to wrap. * @param doEncode true if we should encode all data written to us, * false if we should decode. */ public Base64OutputStream(OutputStream out, boolean doEncode) { super(out); this.doEncode = doEncode; this.base64 = new Base64(); }
int len = Math.min(avail(), bAvail); if (buf != b) { System.arraycopy(buf, readPos, b, bPos, len);
@Nullable public static MessageReference parse(String identity) { if (identity == null || identity.length() < 1 || identity.charAt(0) != IDENTITY_VERSION_1) { return null; } StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false); if (tokens.countTokens() < 3) { return null; } String accountUuid = Base64.decode(tokens.nextToken()); String folderServerId = Base64.decode(tokens.nextToken()); String uid = Base64.decode(tokens.nextToken()); if (!tokens.hasMoreTokens()) { return new MessageReference(accountUuid, folderServerId, uid, null); } Flag flag; try { flag = Flag.valueOf(tokens.nextToken()); } catch (IllegalArgumentException e) { return null; } return new MessageReference(accountUuid, folderServerId, uid, flag); }
/** * Creates a Base64OutputStream such that all data written is either * Base64-encoded or Base64-decoded to the original provided OutputStream. * * @param out OutputStream to wrap. * @param doEncode true if we should encode all data written to us, * false if we should decode. * @param lineLength If doEncode is true, each line of encoded * data will contain lineLength characters. * If lineLength <=0, the encoded data is not divided into lines. * If doEncode is false, lineLength is ignored. * @param lineSeparator If doEncode is true, each line of encoded * data will be terminated with this byte sequence (e.g. \r\n). * If lineLength <= 0, the lineSeparator is not used. * If doEncode is false lineSeparator is ignored. */ public Base64OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) { super(out); this.doEncode = doEncode; this.base64 = new Base64(lineLength, lineSeparator); }
private void saslAuthExternal() throws MessagingException, IOException { executeCommand("AUTH EXTERNAL %s", Base64.encode(username)); }
/** * Writes <code>len</code> bytes from the specified * <code>b</code> array starting at <code>offset</code> to * this output stream. * * @param b source byte array * @param offset where to start reading the bytes * @param len maximum number of bytes to write * * @throws IOException if an I/O error occurs. * @throws NullPointerException if the byte array parameter is null * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid */ @Override public void write(byte b[], int offset, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (offset < 0 || len < 0 || offset + len < 0) { throw new IndexOutOfBoundsException(); } else if (offset > b.length || offset + len > b.length) { throw new IndexOutOfBoundsException(); } else if (len > 0) { if (doEncode) { base64.encode(b, offset, len); } else { base64.decode(b, offset, len); } flush(false); } }
/** * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks * * @param binaryData * binary data to encode * @return Base64 characters chunked in 76 character blocks */ public static byte[] encodeBase64Chunked(byte[] binaryData) { return encodeBase64(binaryData, true); }
public static boolean shouldRetry(String response, String host) { String decodedResponse = Base64.decode(response); if (K9MailLib.isDebug()) { Timber.v("Challenge response: %s", decodedResponse); } try { JSONObject json = new JSONObject(decodedResponse); String status = json.getString("status"); if (!BAD_RESPONSE.equals(status)) { return false; } } catch (JSONException jsonException) { Timber.e("Error decoding JSON response from: %s. Response was: %s", host, decodedResponse); } return true; } }
/** * Encodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the * Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[]. * * @param pObject * Object to encode * @return An object (of type byte[]) containing the base64 encoded data which corresponds to the byte[] supplied. * @throws EncoderException * if the parameter supplied is not of type byte[] */ public Object encode(Object pObject) throws EncoderException { if (!(pObject instanceof byte[])) { throw new EncoderException("Parameter supplied to Base64 encode is not a byte[]"); } return encode((byte[]) pObject); }