@Test public void addEncryptedTextCompile() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2056); KeyPair pair = keyGen.generateKeyPair(); DocumentFile.Builder builder = new DocumentFile.Builder("fakeHash"); builder.appendEncryptedText("Xanadu"); builder.appendEncryptedText("Green"); DocumentFile file = builder.build(); String compiledDocument = DocumentFile.compiler().compileEncrypted(file, new ECKey(), pair.getPublic()); DocumentFile result = DocumentFile.decompiler().decompileEncrypted("fakehash", compiledDocument.getBytes(), pair.getPrivate()); assertEquals("XanaduGreen", result.getEncyptedInvariantStream()); }
@Override public String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key) throws Exception { if (ecKey == null) { throw new IllegalArgumentException("ECKey is null"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); String publicKeyHash = URLDecoder.decode(ecKey.toAddress(MainNetParams.get()).toString(), "UTF-8"); os.write(DocumentFile.MAGIC); os.writeShort(0); os.writeUTF(publicKeyHash); os.writeUTF(doc.hasGenesisHash() ? doc.getGenesisHash() : ""); os.writeUTF(doc.hasHashPreviousBlock() ? doc.getHashPreviousBlock() : ""); os.writeInt(doc.getMajorVersion()); os.writeInt(doc.getMinorVersion()); os.writeLong(System.currentTimeMillis()); os.writeInt(doc.stringPoolSize()); for (Map.Entry<Integer, String> entry : doc.getStringPool().entrySet()) { os.writeInt(entry.getKey()); os.writeUTF(entry.getValue()); } os.writeInt(doc.overlayPoolSize()); for (Map.Entry<Integer, Overlay> entry : doc.getOverlayPool().entrySet()) { os.writeInt(entry.getKey()); os.write(entry.getValue().encode()); } os.writeUTF(doc.hasInvariantStream() ? doc.getInvariantStream() : ""); os.writeUTF(doc.hasEncryptedInvariantStream() ? encrypt(doc.getEncyptedInvariantStream(), key) : ""); os.writeInt(doc.operationCount()); for (Op op : doc.getOps()) { os.write(op.encode()); } String message = base64Url().encode(baos.toByteArray()); String signature64 = ecKey.signMessage(message); return message + "." + publicKeyHash + "." + signature64; }
DocumentFileCompiler implements Compiler<DocumentFile> { @Override public String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key) throws Exception { if (ecKey == null) { throw new IllegalArgumentException("ECKey is null"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); String publicKeyHash = URLDecoder.decode(ecKey.toAddress(MainNetParams.get()).toString(), "UTF-8"); os.write(DocumentFile.MAGIC); os.writeShort(0); os.writeUTF(publicKeyHash); os.writeUTF(doc.hasGenesisHash() ? doc.getGenesisHash() : ""); os.writeUTF(doc.hasHashPreviousBlock() ? doc.getHashPreviousBlock() : ""); os.writeInt(doc.getMajorVersion()); os.writeInt(doc.getMinorVersion()); os.writeLong(System.currentTimeMillis()); os.writeInt(doc.stringPoolSize()); for (Map.Entry<Integer, String> entry : doc.getStringPool().entrySet()) { os.writeInt(entry.getKey()); os.writeUTF(entry.getValue()); } os.writeInt(doc.overlayPoolSize()); for (Map.Entry<Integer, Overlay> entry : doc.getOverlayPool().entrySet()) { os.writeInt(entry.getKey()); os.write(entry.getValue().encode()); } os.writeUTF(doc.hasInvariantStream() ? doc.getInvariantStream() : ""); os.writeUTF(doc.hasEncryptedInvariantStream() ? encrypt(doc.getEncyptedInvariantStream(), key) : ""); os.writeInt(doc.operationCount()); for (Op op : doc.getOps()) { os.write(op.encode()); } String message = base64Url().encode(baos.toByteArray()); String signature64 = ecKey.signMessage(message); return message + "." + publicKeyHash + "." + signature64; } }
DocumentFileCompiler implements Compiler<DocumentFile> { @Override public String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key) throws Exception { if (ecKey == null) { throw new IllegalArgumentException("ECKey is null"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); String publicKeyHash = URLDecoder.decode(ecKey.toAddress(MainNetParams.get()).toString(), "UTF-8"); os.write(DocumentFile.MAGIC); os.writeShort(0); os.writeUTF(publicKeyHash); os.writeUTF(doc.hasGenesisHash() ? doc.getGenesisHash() : ""); os.writeUTF(doc.hasHashPreviousBlock() ? doc.getHashPreviousBlock() : ""); os.writeInt(doc.getMajorVersion()); os.writeInt(doc.getMinorVersion()); os.writeLong(System.currentTimeMillis()); os.writeInt(doc.stringPoolSize()); for (Map.Entry<Integer, String> entry : doc.getStringPool().entrySet()) { os.writeInt(entry.getKey()); os.writeUTF(entry.getValue()); } os.writeInt(doc.overlayPoolSize()); for (Map.Entry<Integer, Overlay> entry : doc.getOverlayPool().entrySet()) { os.writeInt(entry.getKey()); os.write(entry.getValue().encode()); } os.writeUTF(doc.hasInvariantStream() ? doc.getInvariantStream() : ""); os.writeUTF(doc.hasEncryptedInvariantStream() ? encrypt(doc.getEncyptedInvariantStream(), key) : ""); os.writeInt(doc.operationCount()); for (Op op : doc.getOps()) { os.write(op.encode()); } String message = base64Url().encode(baos.toByteArray()); String signature64 = ecKey.signMessage(message); return message + "." + publicKeyHash + "." + signature64; }  }
DocumentFileCompiler implements Compiler<DocumentFile> { @Override public String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key) throws Exception { if (ecKey == null) { throw new IllegalArgumentException("ECKey is null"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); String publicKeyHash = URLDecoder.decode(ecKey.toAddress(MainNetParams.get()).toString(), "UTF-8"); os.write(DocumentFile.MAGIC); os.writeShort(0); os.writeUTF(publicKeyHash); os.writeUTF(doc.hasGenesisHash() ? doc.getGenesisHash() : ""); os.writeUTF(doc.hasHashPreviousBlock() ? doc.getHashPreviousBlock() : ""); os.writeInt(doc.getMajorVersion()); os.writeInt(doc.getMinorVersion()); os.writeLong(System.currentTimeMillis()); os.writeInt(doc.stringPoolSize()); for (Map.Entry<Integer, String> entry : doc.getStringPool().entrySet()) { os.writeInt(entry.getKey()); os.writeUTF(entry.getValue()); } os.writeInt(doc.overlayPoolSize()); for (Map.Entry<Integer, Overlay> entry : doc.getOverlayPool().entrySet()) { os.writeInt(entry.getKey()); os.write(entry.getValue().encode()); } os.writeUTF(doc.hasInvariantStream() ? doc.getInvariantStream() : ""); os.writeUTF(doc.hasEncryptedInvariantStream() ? encrypt(doc.getEncyptedInvariantStream(), key) : ""); os.writeInt(doc.operationCount()); for (Op op : doc.getOps()) { os.write(op.encode()); } String message = base64Url().encode(baos.toByteArray()); String signature64 = ecKey.signMessage(message); return message + "." + publicKeyHash + "." + signature64; }  static DocumentFileCompiler createInstance(); @Override String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key); }
DocumentFileCompiler implements Compiler<DocumentFile> { @Override public String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key) throws Exception { if (ecKey == null) { throw new IllegalArgumentException("ECKey is null"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(baos); String publicKeyHash = URLDecoder.decode(ecKey.toAddress(MainNetParams.get()).toString(), "UTF-8"); os.write(DocumentFile.MAGIC); os.writeShort(0); os.writeUTF(publicKeyHash); os.writeUTF(doc.hasGenesisHash() ? doc.getGenesisHash() : ""); os.writeUTF(doc.hasHashPreviousBlock() ? doc.getHashPreviousBlock() : ""); os.writeInt(doc.getMajorVersion()); os.writeInt(doc.getMinorVersion()); os.writeLong(System.currentTimeMillis()); os.writeInt(doc.stringPoolSize()); for (Map.Entry<Integer, String> entry : doc.getStringPool().entrySet()) { os.writeInt(entry.getKey()); os.writeUTF(entry.getValue()); } os.writeInt(doc.overlayPoolSize()); for (Map.Entry<Integer, Overlay> entry : doc.getOverlayPool().entrySet()) { os.writeInt(entry.getKey()); os.write(entry.getValue().encode()); } os.writeUTF(doc.hasInvariantStream() ? doc.getInvariantStream() : ""); os.writeUTF(doc.hasEncryptedInvariantStream() ? encrypt(doc.getEncyptedInvariantStream(), key) : ""); os.writeInt(doc.operationCount()); for (Op op : doc.getOps()) { os.write(op.encode()); } String message = base64Url().encode(baos.toByteArray()); String signature64 = ecKey.signMessage(message); return message + "." + publicKeyHash + "." + signature64; }  static DocumentFileCompiler createInstance(); @Override String compileEncrypted(DocumentFile doc, ECKey ecKey, Key key);  }