-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add CID.encodedLength() & remove leading uint32 CID length from encoded aes form #60
Conversation
Re performance - this means there's a double-decode of 3 varints, once to figure out the length and another to do the decode. An alternative method that would avoid this is to allow |
Instead of changing |
The use-case I have for this in I'm not saying Some of the relevant code for the streaming case is here https://github.com/ipld/js-car/blob/16d11873a33ae1935beb17847de6fbb90faba1d3/lib/decoder.js#L53-L69 . Note how I have a wrapper around the incoming stream data that lets me request |
There's a why not both? in here perhaps - maybe an |
Ya, probably best to just return all the data you’ve parsed that could possibly be useful since you went and parsed it already anyway :)
…-Mikeal
________________________________
From: Rod Vagg <[email protected]>
Sent: Wednesday, January 20, 2021 3:05:51 PM
To: multiformats/js-multiformats <[email protected]>
Cc: Mikeal Rogers <[email protected]>; Comment <[email protected]>
Subject: Re: [multiformats/js-multiformats] add CID.encodedLength() & remove leading uint32 CID length from encoded aes form (#60)
There's a why not both? in here perhaps - maybe an inspectBytes() that returns an object that has the initial varints and a calculation of the total length. CID.inspectBytes(initialBytes) -> { version, codec, multihashCode, multihashLength, prefixLength } - I could use that to combine prefixLength and multihashLength to figure out what I need. Internally this could be the start of decode() and fromBytes() so we reduce varint decode duplication. And it could also be used in cases where you want to peek into a CID without fully decoding it to check on it -- perhaps you want to check if the bytes you have are a DAG-CBOR+Blake2-256 and that's all you care about.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<#60 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAAAEQ7AKSP3XL3U7IH37QTS25OU7ANCNFSM4WCIFXJA>.
|
09a6573
to
9d83ab8
Compare
OK, so I'm trying to reconcile API complexity and naming concerns here, but this is what I've come up with:
One major change internally is that it now bypasses |
9d83ab8
to
80ba05f
Compare
I've rebased this against For the record (so it can be re-applied later), the additional diff for the crypto PR #59 that I've removed is: diff --git a/src/codecs/aes.js b/src/codecs/aes.js
index cd14de5..e46c236 100644
--- a/src/codecs/aes.js
+++ b/src/codecs/aes.js
@@ -3,24 +3,6 @@ import aes from 'js-crypto-aes'
import CID from '../cid.js'
import { codec } from './codec.js'
-const enc32 = value => {
- value = +value
- const buff = new Uint8Array(4)
- buff[3] = (value >>> 24)
- buff[2] = (value >>> 16)
- buff[1] = (value >>> 8)
- buff[0] = (value & 0xff)
- return buff
-}
-
-const readUInt32LE = (buffer) => {
- const offset = buffer.byteLength - 4
- return ((buffer[offset]) |
- (buffer[offset + 1] << 8) |
- (buffer[offset + 2] << 16)) +
- (buffer[offset + 3] * 0x1000000)
-}
-
let code = 0x1400
const concat = buffers => Uint8Array.from(buffers.map(b => [...b]).flat())
@@ -37,18 +19,17 @@ const mkcrypto = ({ name, code, ivsize }) => {
const decrypt = async ({ key, value }) => {
let { bytes, iv } = value
bytes = await aes.decrypt(bytes, key, { name: name.toUpperCase(), iv, tagLength: 16 })
- const len = readUInt32LE(bytes.subarray(0, 4))
- const cid = CID.decode(bytes.subarray(4, 4 + len))
- bytes = bytes.subarray(4 + len)
- return { cid, bytes }
+ const [cid, remainder] = CID.decodeFirst(bytes)
+ return { cid, bytes: remainder }
}
+
const encrypt = async ({ key, cid, bytes }) => {
- const len = enc32(cid.bytes.byteLength)
const iv = random.getRandomBytes(ivsize)
- const msg = concat([len, cid.bytes, bytes])
+ const msg = concat([cid.bytes, bytes])
bytes = await aes.encrypt(msg, key, { name: name.toUpperCase(), iv, tagLength: 16 })
return { bytes, iv }
}
+
return { encode, decode, encrypt, decrypt, code, name: name.toLowerCase() }
} |
80ba05f
to
8e57fdd
Compare
Looks great! :) |
There's two commits in here, the first one could go against
master
but it's used for the second one which is intended forcrypto
, #349. If this is an acceptable approach I'll write up some additional tests to exerciseencodedLength()
a bit more (CIDv0 and bad bytes - and these would be good to do with the aes tests too).Ref: ipld/specs#349 (comment)