Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Crypto: Cipher.read([size]) disregard size at end #9188

Closed
Jimmy-Z opened this issue Feb 11, 2015 · 1 comment
Closed

Crypto: Cipher.read([size]) disregard size at end #9188

Jimmy-Z opened this issue Feb 11, 2015 · 1 comment

Comments

@Jimmy-Z
Copy link

Jimmy-Z commented Feb 11, 2015

long story(about how I discovered this) short, this is the test code:

console.log(process.version);

var cipher = require("crypto").createCipher("rc4", "1111");

cipher.on("readable", function(){
    var r = cipher.read(2);
    if(r == null){
        console.log("readable: no enough data to read");
    }else{
        console.log("readable: r.length = " + r.length);
    }
});

cipher.on("end", function(){
    console.log("end");
});

cipher.write("11");
cipher.write("1");
setTimeout(function(){
    console.log("firing end");
    cipher.end();
}, 1000);

the output is:

v0.12.0
readable: r.length = 2
readable: no enough data to read
firing end
readable: r.length = 1
end

according to the document, when Stream.read is call with size, "If size bytes are not available, then it will return null." but clearly when readable is triggered by end it will disregard the size parameter.

I don't know if this is Cipher only or all Stream behaves like this, I just don't know any more built-in read/writable stream class.

if this is by design, please update the documentation.

@jasnell jasnell added the crypto label Jun 25, 2015
@brendanashworth
Copy link

@Twilight thanks for opening an issue. This was fixed in nodejs/node@936c9ff, with the new text:

If you pass in a size argument, then it will return that many
bytes. If size bytes are not available, then it will return null,
unless we've ended, in which case it will return the data remaining
in the buffer.

I believe this has been backported to v0.12, so I'll close this for now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants