exceptionfactory / jagged Goto Github PK
View Code? Open in Web Editor NEWJava implementation of age encryption
License: Apache License 2.0
Java implementation of age encryption
License: Apache License 2.0
I have a problem with small files or short strings. The output of the encryption seems to be truncated. The attached junit fails, because encrypted.length = 65720.
jagged version: 0.3.2
maven.compiler.release: 17
@Test
void shouldEncrypt66000() throws Exception {
String content = IntStream.range(0, 66000).boxed()
.map(i -> "1")
.collect(Collectors.joining());
verify(content);
}
void verify(final String content) throws Exception {
byte[] encrypted;
String decrypted;
System.out.printf("input content length: %s%n", content.length());
final KeyPairGenerator keyPairGenerator = new X25519KeyPairGenerator();
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
final RecipientStanzaWriter stanzaWriter = X25519RecipientStanzaWriterFactory.newRecipientStanzaWriter(keyPair.getPublic().toString());
final EncryptingChannelFactory encryptingChannelFactory = new StandardEncryptingChannelFactory();
try (
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ReadableByteChannel inputChannel =
Channels.newChannel(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
final WritableByteChannel encryptingChannel = encryptingChannelFactory.newEncryptingChannel(
Channels.newChannel(new BufferedOutputStream(out)),
Collections.singletonList(stanzaWriter))) {
copy(inputChannel, encryptingChannel);
out.flush();
encrypted = out.toByteArray();
}
System.out.printf("encrypted length: %s%n", encrypted.length);
assertThat(encrypted.length)
.isGreaterThan(content.length());
}
// from README:
void copy(
final ReadableByteChannel inputChannel,
final WritableByteChannel outputChannel) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(65536);
int read = 0;
int wrote = 0;
while (inputChannel.read(buffer) > 0) {
buffer.flip();
read += buffer.limit();
//System.out.printf("read: %s ", read);
while (buffer.hasRemaining()) {
wrote += outputChannel.write(buffer);
//System.out.printf("wrote: %s%n", read);
}
buffer.clear();
}
}
I've been trying to pinpoint a weird behaviour I have with this library.
If I decrypt a file from disk it works fine using the example in the README (had to dig up your copy implementation which I found on your blog).
If I decrypt a file I get on the fly from the network (from an s3 bucket in my case), the output is truncated leading either to 0 length decrypted files or truncated files.
I've pinpointed this to a weird behaviour I'm seeing here :
if readPlainBuffer
returns -1
, the content read in previous iterations of the loop is thrown away and read
is set to EOF, basically ignoring all the data that's been read so far, BUT, that data has been written into the outputBuffer
, thus the caller's ByteBuffer
has changed but the method returns that it hasn't.
My guess is that you tested with files small enough to not cause read(buffer) to fill the buffer, but when reading from the network, things work slightly differently (buffers are not always filled up, read can return 0 in some cases, etc.).
The code is complex and I might be misunderstanding what's happening, for now, I've replaced jagged with a stream that passes data to the age
binary using ProcessBuilder and everything works (pointing to jagged doing something wrong).
Hello. I'm trying to use this with the old java.io streams and when I use Channels.newChannel/newOutputStream it causes issues when everything is to be closed. I made a unit test to reproduce what I am seeing. I'll send it as a PR.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.