Skip to content

Commit

Permalink
fix: revert changes from 4c1a89f (Separate API for sign/verify) and 0…
Browse files Browse the repository at this point in the history
…a6c207(UTF8 encoding/decoding) which break tests
  • Loading branch information
NexZhu committed Jun 5, 2021
1 parent 0c2a865 commit 95fdef1
Showing 1 changed file with 51 additions and 87 deletions.
138 changes: 51 additions & 87 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,75 +118,19 @@ export default {

// Converts a string to a byte array.
string2bytes(str) {
const utf8 = []
const bytes = []
for (let i = 0; i < str.length; i++) {
let charcode = str.charCodeAt(i)
if (charcode < 0x80) utf8.push(charcode)
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f))
} else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(
0xe0 | (charcode >> 12),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f),
)
}
// surrogate pair
else {
i++
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode =
0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff))
utf8.push(
0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f),
)
}
bytes.push(str.charCodeAt(i))
}
return utf8
return bytes
},

// Converts a byte array to a string.
bytes2string(data) {
let str = '',
i

for (i = 0; i < data.length; i++) {
const value = data[i]

if (value < 0x80) {
str += String.fromCharCode(value)
} else if (value > 0xbf && value < 0xe0) {
str += String.fromCharCode(((value & 0x1f) << 6) | (data[i + 1] & 0x3f))
i += 1
} else if (value > 0xdf && value < 0xf0) {
str += String.fromCharCode(
((value & 0x0f) << 12) |
((data[i + 1] & 0x3f) << 6) |
(data[i + 2] & 0x3f),
)
i += 2
} else {
// surrogate pair
const charCode =
(((value & 0x07) << 18) |
((data[i + 1] & 0x3f) << 12) |
((data[i + 2] & 0x3f) << 6) |
(data[i + 3] & 0x3f)) -
0x010000

str += String.fromCharCode(
(charCode >> 10) | 0xd800,
(charCode & 0x03ff) | 0xdc00,
)
i += 3
}
bytes2string(bytes) {
let str = ''
for (let i = 0; i < bytes.length; i++) {
str += String.fromCharCode(bytes[i])
}

return str
},

Expand Down Expand Up @@ -308,38 +252,58 @@ export default {
},

encrypt(plaintext, publickeystring, signingkey) {
let cipherblock = ''
const aeskey = this.generateAESKey()
try {
const publickey = this.publicKeyFromString(publickeystring)
cipherblock +=
this.b16to64(publickey.encrypt(this.bytes2string(aeskey))) + '?'
} catch (err) {
return {
status: 'Invalid public key',
{
let cipherblock = ''
const aeskey = this.generateAESKey()
try {
const publickey = this.publicKeyFromString(publickeystring)
cipherblock +=
this.b16to64(publickey.encrypt(this.bytes2string(aeskey))) + '?'
} catch (err) {
return { status: 'Invalid public key' }
}
}
if (signingkey) {
plaintext += this.sign(plaintext, signingkey)
}
cipherblock += this.encryptAESCBC(plaintext, aeskey)
return {
status: 'success',
cipher: cipherblock,
if (signingkey) {
const signString = cryptico.b16to64(
signingkey.signString(plaintext, 'sha256'),
)
plaintext += '::52cee64bb3a38f6403386519a39ac91c::'
plaintext += cryptico.publicKeyString(signingkey)
plaintext += '::52cee64bb3a38f6403386519a39ac91c::'
plaintext += signString
}
cipherblock += this.encryptAESCBC(plaintext, aeskey)
return { status: 'success', cipher: cipherblock }
}
},

decrypt(ciphertext, key) {
const cipherblock = ciphertext.split('?')
let aeskey = key.decrypt(this.b64to16(cipherblock[0]))
if (!aeskey) {
return {
status: 'failure',
}
if (aeskey == null) {
return { status: 'failure' }
}
aeskey = this.string2bytes(aeskey)
const plaintext = this.decryptAESCBC(cipherblock[1], aeskey)
if (plaintext.indexOf('::52cee64bb3a38f6403386519a39ac91c::') !== -1) {
return this._confirm(plaintext)
const plaintext = this
.decryptAESCBC(cipherblock[1], aeskey)
.split('::52cee64bb3a38f6403386519a39ac91c::')
if (plaintext.length === 3) {
const publickey = this.publicKeyFromString(plaintext[1])
const signature = this.b64to16(plaintext[2])
if (publickey.verifyString(plaintext[0], signature)) {
return {
status: 'success',
plaintext: plaintext[0],
signature: 'verified',
publicKeyString: this.publicKeyString(publickey),
}
} else {
return {
status: 'success',
plaintext: plaintext[0],
signature: 'forged',
publicKeyString: this.publicKeyString(publickey),
}
}
} else {
return {
status: 'success',
Expand Down

0 comments on commit 95fdef1

Please sign in to comment.