public static void startJweContent(OutputStream os, JweHeaders headers, byte[] encryptedContentEncryptionKey, byte[] cipherInitVector) throws IOException { byte[] jsonBytes = StringUtils.toBytesUTF8(getHeadersJson(headers)); Base64UrlUtility.encodeAndStream(jsonBytes, 0, jsonBytes.length, os); byte[] dotBytes = new byte[]{'.'}; os.write(dotBytes); Base64UrlUtility.encodeAndStream(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length, os); os.write(dotBytes); Base64UrlUtility.encodeAndStream(cipherInitVector, 0, cipherInitVector.length, os); os.write(dotBytes); os.flush(); }
public static void startJweContent(OutputStream os, JweHeaders headers, byte[] encryptedContentEncryptionKey, byte[] cipherInitVector) throws IOException { byte[] jsonBytes = StringUtils.toBytesUTF8(getHeadersJson(headers)); Base64UrlUtility.encodeAndStream(jsonBytes, 0, jsonBytes.length, os); byte[] dotBytes = new byte[]{'.'}; os.write(dotBytes); Base64UrlUtility.encodeAndStream(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length, os); os.write(dotBytes); Base64UrlUtility.encodeAndStream(cipherInitVector, 0, cipherInitVector.length, os); os.write(dotBytes); os.flush(); }
@Override public void flush() throws IOException { if (flushed) { return; } if (writeSignature) { byte[] finalBytes = signature.sign(); out.write(new byte[]{'.'}); Base64UrlUtility.encodeAndStream(finalBytes, 0, finalBytes.length, out); } else { super.flush(); } flushed = true; }
@Override public void flush() throws IOException { if (flushed) { return; } if (writeSignature) { byte[] finalBytes = signature.sign(); out.write(new byte[]{'.'}); Base64UrlUtility.encodeAndStream(finalBytes, 0, finalBytes.length, out); } else { super.flush(); } flushed = true; }
private void encodeAndWrite(byte[] encryptedChunk, int off, int len, boolean finalWrite) throws IOException { byte[] theChunk = lastEncryptedDataChunk; int lenToEncode = len; if (theChunk != null) { theChunk = newArray(theChunk, 0, theChunk.length, encryptedChunk, off, len); lenToEncode = theChunk.length; off = 0; } else { theChunk = encryptedChunk; } int rem = finalWrite ? 0 : lenToEncode % 3; Base64UrlUtility.encodeAndStream(theChunk, off, lenToEncode - rem, out); out.flush(); if (rem > 0) { lastEncryptedDataChunk = newArray(theChunk, lenToEncode - rem, rem); } else { lastEncryptedDataChunk = null; } }
private void encodeAndWrite(byte[] encryptedChunk, int off, int len, boolean finalWrite) throws IOException { byte[] theChunk = lastEncryptedDataChunk; int lenToEncode = len; if (theChunk != null) { theChunk = newArray(theChunk, 0, theChunk.length, encryptedChunk, off, len); lenToEncode = theChunk.length; off = 0; } else { theChunk = encryptedChunk; } int rem = finalWrite ? 0 : lenToEncode % 3; Base64UrlUtility.encodeAndStream(theChunk, off, lenToEncode - rem, out); out.flush(); if (rem > 0) { lastEncryptedDataChunk = newArray(theChunk, lenToEncode - rem, rem); } else { lastEncryptedDataChunk = null; } }
@Override public void flush() throws IOException { if (flushed) { return; } out.write(StringUtils.toBytesUTF8("\",\"signatures\":[")); shutdownExecutor(); for (int i = 0; i < signatures.size(); i++) { if (i > 0) { out.write(new byte[]{','}); } out.write(StringUtils.toBytesUTF8("{\"protected\":\"" + protectedHeaders.get(i) + "\",\"signature\":\"")); byte[] sign = signatures.get(i).sign(); Base64UrlUtility.encodeAndStream(sign, 0, sign.length, out); out.write(StringUtils.toBytesUTF8("\"}")); } out.write(StringUtils.toBytesUTF8("]}")); flushed = true; } private void shutdownExecutor() {
@Override public void flush() throws IOException { if (flushed) { return; } out.write(StringUtils.toBytesUTF8("\",\"signatures\":[")); shutdownExecutor(); for (int i = 0; i < signatures.size(); i++) { if (i > 0) { out.write(new byte[]{','}); } out.write(StringUtils.toBytesUTF8("{\"protected\":\"" + protectedHeaders.get(i) + "\",\"signature\":\"")); byte[] sign = signatures.get(i).sign(); Base64UrlUtility.encodeAndStream(sign, 0, sign.length, out); out.write(StringUtils.toBytesUTF8("\"}")); } out.write(StringUtils.toBytesUTF8("]}")); flushed = true; } private void shutdownExecutor() {
@Override public void writeTo(JwsDetachedSignature parts, Class<?> cls, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { JoseUtils.traceHeaders(parts.getHeaders()); byte[] finalBytes = parts.getSignature().sign(); if (!parts.isUseJwsJsonSignatureFormat()) { os.write(StringUtils.toBytesASCII(parts.getEncodedHeaders())); byte[] dotBytes = new byte[]{'.'}; os.write(dotBytes); os.write(dotBytes); Base64UrlUtility.encodeAndStream(finalBytes, 0, finalBytes.length, os); } else { // use flattened JWS JSON format os.write(StringUtils.toBytesASCII("{")); String headersProp = "\"protected\":\"" + parts.getEncodedHeaders() + "\""; os.write(StringUtils.toBytesUTF8(headersProp)); os.write(StringUtils.toBytesASCII(",")); String sigProp = "\"signature\":\"" + Base64UrlUtility.encode(finalBytes) + "\""; os.write(StringUtils.toBytesUTF8(sigProp)); os.write(StringUtils.toBytesASCII("}")); } }
JwsOutputStream jwsStream = new JwsOutputStream(actualOs, jwsSignature, true); byte[] headerBytes = StringUtils.toBytesUTF8(writer.toJson(headers)); Base64UrlUtility.encodeAndStream(headerBytes, 0, headerBytes.length, jwsStream); jwsStream.write(new byte[]{'.'});