Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ class Hypercore extends EventEmitter {
if (this.keyPair === null) this.keyPair = opts.keyPair || this.core.header.keyPair

const parent = opts.parent || null
if (parent && parent.encryption) this.encryption = parent.encryption
const encryption = getEncryptionOption(opts)

const e = getEncryptionOption(opts)
if (!this.encryption) this.encryption = this._getEncryptionProvider(e)
if (encryption) this.encryption = this._getEncryptionProvider(encryption)
else if (parent && parent.encryption) this.encryption = parent.encryption

this.writable = this._isWritable()

Expand Down
41 changes: 41 additions & 0 deletions test/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,47 @@ test('block encryption module', async function (t) {
t.alike(await core.get(2), b4a.from('2'))
})

test('encryption provider is set on session', async function (t) {
class XOREncryption {
padding() {
return 0
}

async encrypt(index, block) {
await new Promise(setImmediate)

for (let i = 0; i < block.byteLength; i++) {
block[i] ^= (index + 1) & 0xff // +1 so no 0 xor in test
}
}

async decrypt(index, block) {
await new Promise(setImmediate)

for (let i = 0; i < block.byteLength; i++) {
block[i] ^= (index + 1) & 0xff
}
}
}

const encryption1 = new XOREncryption()
const encryption2 = new XOREncryption()

const core = await create(t, null, { encryption: encryption1 })
await core.ready()

await core.append('0')
await core.append('1')
await core.append('2')

const session = core.session({ encryption: encryption2 })
await session.ready()

t.not(core.encryption, session.encryption)

await session.close()
})

test('encryption backwards compatibility', async function (t) {
const encryptionKey = b4a.alloc(32).fill('encryption key')

Expand Down