Giter VIP home page Giter VIP logo

jagged's People

Contributors

exceptionfactory avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jagged's Issues

Data encryption problem for a small dataset

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();
        }
    }

Truncated output when reading data not readily available

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 (END_OF_FILE == plainBufferRead) {
read = END_OF_FILE;
break;
} else {
read += plainBufferRead;
}

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.