Skip to content

Commit

Permalink
refactor: remove unused asymmetric encode/decode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed May 15, 2021
1 parent 308d465 commit ab982da
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 86 deletions.
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"buffer": "^6.0.3",
"cross-fetch": "^3.1.4",
"crypto-browserify": "^3.12.0",
"ed2curve": "^0.3.0",
"joi-browser": "^13.4.0",
"libsodium-wrappers-sumo": "0.7.9",
"path-browserify": "^1.0.1",
Expand Down
51 changes: 0 additions & 51 deletions src/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import bs58check from 'bs58check'
import { decode as rlpDecode, encode as rlpEncode } from 'rlp'
import { blake2b } from 'blakejs'
import ed2curve from 'ed2curve'
import nacl from 'tweetnacl'
import aesjs from 'aes-js'
import shajs from 'sha.js'
Expand Down Expand Up @@ -482,53 +481,3 @@ export function isValidKeypair (privateKey, publicKey) {
const signature = sign(message, privateKey)
return verify(message, signature, publicKey)
}

/**
* This function encrypts a message using base58check encoded and 'ak' prefixed
* publicKey such that only the corresponding secretKey will
* be able to decrypt
* @rtype (msg: String, publicKey: String) => Object
* @param {Buffer} msg - Data to encode
* @param {String} publicKey - Public key
* @return {Object}
*/
export function encryptData (msg, publicKey) {
const ephemeralKeyPair = nacl.box.keyPair()
const pubKeyUInt8Array = decodeBase58Check(assertedType(publicKey, 'ak'))
const nonce = nacl.randomBytes(nacl.box.nonceLength)

const encryptedMessage = nacl.box(
Buffer.from(msg),
nonce,
ed2curve.convertPublicKey(pubKeyUInt8Array),
ephemeralKeyPair.secretKey
)

return {
ciphertext: Buffer.from(encryptedMessage).toString('hex'),
ephemPubKey: Buffer.from(ephemeralKeyPair.publicKey).toString('hex'),
nonce: Buffer.from(nonce).toString('hex'),
version: 'x25519-xsalsa20-poly1305'
}
}

/**
* This function decrypt a message using secret key
* @rtype (secretKey: String, encryptedData: Object) => Buffer|null
* @param {String} secretKey - Secret key
* @param {Object} encryptedData - Encrypted data
* @return {Buffer|null}
*/
export function decryptData (secretKey, encryptedData) {
const receiverSecretKeyUint8Array = ed2curve.convertSecretKey(Buffer.from(secretKey, 'hex'))
const nonce = Buffer.from(encryptedData.nonce, 'hex')
const ciphertext = Buffer.from(encryptedData.ciphertext, 'hex')
const ephemPubKey = Buffer.from(encryptedData.ephemPubKey, 'hex')
const decrypted = nacl.box.open(
ciphertext,
nonce,
ephemPubKey,
receiverSecretKeyUint8Array
)
return decrypted ? Buffer.from(decrypted) : decrypted
}
26 changes: 0 additions & 26 deletions test/unit/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,30 +161,4 @@ describe('crypto', () => {
buildTxHash(txRaw).should.be.equal(expectedHash)
buildTxHash(rlpEncodedTx).should.be.equal(expectedHash)
})
describe('Encrypt data using nacl box asymmetric encryption', async () => {
const { publicKey, secretKey } = Crypto.generateKeyPair()
const msgString = 'Test string'
const msgBuffer = Buffer.from(msgString)

it('Encrypt String/Buffer and decrypt', () => {
const encryptedString = Crypto.encryptData(msgString, publicKey)
const encryptedBuffer = Crypto.encryptData(msgBuffer, publicKey)

const decryptedString = Crypto.decryptData(secretKey, encryptedString)
const decryptedBuffer = Crypto.decryptData(secretKey, encryptedBuffer)
Buffer.from(decryptedString).toString().should.be.equal(msgString)
decryptedBuffer.equals(msgBuffer).should.be.equal(true)
})
it('Decrypt with wrong secret', () => {
const keyPair = Crypto.generateKeyPair()

const encryptedString = Crypto.encryptData(msgString, keyPair.publicKey)
const encryptedBuffer = Crypto.encryptData(msgBuffer, keyPair.publicKey)

const decryptedString = Crypto.decryptData(secretKey, encryptedString)
const decryptedBuffer = Crypto.decryptData(secretKey, encryptedBuffer)
const isNull = decryptedBuffer === null && decryptedString === null
isNull.should.be.equal(true)
})
})
})

0 comments on commit ab982da

Please sign in to comment.