Skip to content

Commit

Permalink
nip44 padding update
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Sep 27, 2023
1 parent 7019756 commit 6017c47
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nip44.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const utils = {
if (!Number.isSafeInteger(len) || len < 0) throw new Error('expected positive integer')
if (len <= 32) return 32
const nextpower = 1 << (Math.floor(Math.log2(len - 1)) + 1)
const chunk = nextpower < 256 ? 32 : nextpower / 8
const chunk = nextpower <= 256 ? 32 : nextpower / 8
return chunk * (Math.floor((len - 1) / chunk) + 1)
},

Expand All @@ -48,9 +48,12 @@ export const utils = {
},

unpad(padded: Uint8Array): string {
const unpaddedLength = new DataView(padded.buffer).getUint16(0)
const plaintextBytes = padded.subarray(2, 2 + unpaddedLength)
return utf8Decoder.decode(plaintextBytes)
const unpaddedLen = new DataView(padded.buffer).getUint16(0)
const plaintextB = padded.subarray(2, 2 + unpaddedLen)
const pbLen = plaintextB.length
const expectedTotalLen = 2 + utils.v1.calcPadding(pbLen)
if (pbLen !== unpaddedLen || padded.length !== expectedTotalLen) throw new Error('invalid padding')
return utf8Decoder.decode(plaintextB)
},
},
}
Expand Down

0 comments on commit 6017c47

Please sign in to comment.