From d97336d4082f6a718b1275026745c9dc6429a1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cfengweiqiang-coder=E2=80=9D?= <“weiqiang@imkey.im”> Date: Fri, 3 Jul 2020 12:43:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E7=BB=91=E5=AE=9A=E7=A0=81=E5=B9=B6=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/crypto.js | 53 +++ src/api/devicemanagerapi.js | 651 +++++++++++++++++++----------------- src/main/index.js | 8 + 3 files changed, 412 insertions(+), 300 deletions(-) create mode 100644 src/api/crypto.js diff --git a/src/api/crypto.js b/src/api/crypto.js new file mode 100644 index 0000000..927fadc --- /dev/null +++ b/src/api/crypto.js @@ -0,0 +1,53 @@ + +import crypto from 'crypto' + +// /!\ changing those presets would lock out users with already encrypted databases due to breaking changes. +const ENCRYPTION_ALGORITHM = 'aes-256-cbc' +const IV_LENGTH = 16 +const PBKDF2_ITERATIONS = 10000 +const PBKDF2_KEY_LENGTH = 32 +const PBKDF2_DIGEST = 'sha512' + +export const encryptData = (data, encryptionKey) => { + // in any case, we save new data using an initialization vector + const initializationVector = crypto.randomBytes(IV_LENGTH) + const password = crypto.pbkdf2Sync( + encryptionKey, + initializationVector.toString(), + PBKDF2_ITERATIONS, + PBKDF2_KEY_LENGTH, + PBKDF2_DIGEST + ) + const cipher = crypto.createCipheriv(ENCRYPTION_ALGORITHM, password, initializationVector) + return Buffer.concat([ + initializationVector, + Buffer.from(':'), + cipher.update(data, 'utf8'), + cipher.final() + ]).toString('base64') +} + +export const decryptData = (raw, encryptionKey) => { + const data = Buffer.from(raw, 'base64') + + // We check if the data include an initialization vector + if (data.slice(IV_LENGTH, IV_LENGTH + 1).toString() === ':') { + const initializationVector = data.slice(0, IV_LENGTH) + const password = crypto.pbkdf2Sync( + encryptionKey, + initializationVector.toString(), + PBKDF2_ITERATIONS, + PBKDF2_KEY_LENGTH, + PBKDF2_DIGEST + ) + const decipher = crypto.createDecipheriv(ENCRYPTION_ALGORITHM, password, initializationVector) + return Buffer.concat([decipher.update(data.slice(IV_LENGTH + 1)), decipher.final()]).toString( + 'utf8' + ) + } + + // if not, then we fallback to the deprecated API + // eslint-disable-next-line node/no-deprecated-api + const decipher = crypto.createDecipher(ENCRYPTION_ALGORITHM, encryptionKey) + return Buffer.concat([decipher.update(data), decipher.final()]).toString('utf8') +} diff --git a/src/api/devicemanagerapi.js b/src/api/devicemanagerapi.js index fe7f54f..ae63ad6 100644 --- a/src/api/devicemanagerapi.js +++ b/src/api/devicemanagerapi.js @@ -3,337 +3,388 @@ const devicePb = require('../proto/device_pb') const callImKeyCore = require('./callimkeycore') const constants = require('../common/constants') const _ = require('lodash') +const crypto = require('./crypto') +const fs = require('fs') -export function connect (deviceModelName) { - const request = new devicePb.DeviceConnectReq() - request.setDeviceModelName(deviceModelName) - const requestBytes = request.serializeBinary() - const any = new proto.google.protobuf.Any() - any.setValue(requestBytes) - const imKeyAction = new apiPb.ImkeyAction() - imKeyAction.setMethod('device_connect') - imKeyAction.setParam(any) - const imKeyActionBytes = imKeyAction.serializeBinary() - const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) - const error = callImKeyCore.getLastErrorMessage() - if (error === '' || error === null) { - const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) - return { - isSuccess: true, - result: response.getResult() - } - } else { - const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) - return { - isSuccess: false, - result: errorResponse.getError() +export function connect(deviceModelName) { + const request = new devicePb.DeviceConnectReq() + request.setDeviceModelName(deviceModelName) + const requestBytes = request.serializeBinary() + const any = new proto.google.protobuf.Any() + any.setValue(requestBytes) + const imKeyAction = new apiPb.ImkeyAction() + imKeyAction.setMethod('device_connect') + imKeyAction.setParam(any) + const imKeyActionBytes = imKeyAction.serializeBinary() + const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) + const error = callImKeyCore.getLastErrorMessage() + if (error === '' || error === null) { + const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) + return { + isSuccess: true, + result: response.getResult() + } + } else { + const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) + return { + isSuccess: false, + result: errorResponse.getError() + } } - } } -function getDeviceManageFunction (method_) { - const imKeyAction = new apiPb.ImkeyAction() - imKeyAction.setMethod(method_) - const imKeyActionBytes = imKeyAction.serializeBinary() - const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) - const error = callImKeyCore.getLastErrorMessage() - let result - if (error === '' || error === null) { - if (method_ === 'get_seid') { - const response = new devicePb.GetSeidRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getSeid() - } else if (method_ === 'get_sn') { - const response = new devicePb.GetSnRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getSn() - } else if (method_ === 'get_ram_size') { - const response = new devicePb.GetRamSizeRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getRamSize() - } else if (method_ === 'get_firmware_version') { - const response = new devicePb.GetFirmwareVersionRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getFirmwareVersion() - } else if (method_ === 'get_sdk_info') { - const response = new devicePb.GetSdkInfoRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getSdkVersion() - } else if (method_ === 'check_update') { - const response = new devicePb.CheckUpdateRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.toObject() - } else if (method_ === 'cos_check_update') { - const response = new devicePb.CosCheckUpdateRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.toObject() - } else if (method_ === 'is_bl_status') { - const response = new devicePb.IsBlStatusRes.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getCheckResult() - } else { // method_ === "device_activate"||"device_secure_check"||"bind_display_code" - const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) - result = response.getResult() - } - return { - isSuccess: true, - result: result - } - } else { - const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) - return { - isSuccess: false, - result: errorResponse.getError() +function getDeviceManageFunction(method_) { + const imKeyAction = new apiPb.ImkeyAction() + imKeyAction.setMethod(method_) + const imKeyActionBytes = imKeyAction.serializeBinary() + const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) + const error = callImKeyCore.getLastErrorMessage() + let result + if (error === '' || error === null) { + if (method_ === 'get_seid') { + const response = new devicePb.GetSeidRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getSeid() + } else if (method_ === 'get_sn') { + const response = new devicePb.GetSnRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getSn() + } else if (method_ === 'get_ram_size') { + const response = new devicePb.GetRamSizeRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getRamSize() + } else if (method_ === 'get_firmware_version') { + const response = new devicePb.GetFirmwareVersionRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getFirmwareVersion() + } else if (method_ === 'get_sdk_info') { + const response = new devicePb.GetSdkInfoRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getSdkVersion() + } else if (method_ === 'check_update') { + const response = new devicePb.CheckUpdateRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.toObject() + } else if (method_ === 'cos_check_update') { + const response = new devicePb.CosCheckUpdateRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.toObject() + } else if (method_ === 'is_bl_status') { + const response = new devicePb.IsBlStatusRes.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getCheckResult() + } else { // method_ === "device_activate"||"device_secure_check"||"bind_display_code" + const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) + result = response.getResult() + } + return { + isSuccess: true, + result: result + } + } else { + const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) + return { + isSuccess: false, + result: errorResponse.getError() + } } - } } -function appletManage (method_, appName) { - let request - if (method_ === 'app_download') { - request = new devicePb.AppDownloadReq() - request.setAppName(appName) - } - if (method_ === 'app_update') { - request = new devicePb.AppUpdateReq() - request.setAppName(appName) - } - if (method_ === 'app_delete') { - request = new devicePb.AppDeleteReq() - request.setAppName(appName) - } - const requestBytes = request.serializeBinary() - const any = new proto.google.protobuf.Any() - any.setValue(requestBytes) - const imKeyAction = new apiPb.ImkeyAction() - imKeyAction.setMethod(method_) - imKeyAction.setParam(any) - const imKeyActionBytes = imKeyAction.serializeBinary() - const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) - const error = callImKeyCore.getLastErrorMessage() - if (error === '' || error === null) { - const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) - return { - isSuccess: true, - result: response.getResult() +function appletManage(method_, appName) { + let request + if (method_ === 'app_download') { + request = new devicePb.AppDownloadReq() + request.setAppName(appName) } - } else { - const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) - return { - isSuccess: false, - result: errorResponse.getError() + if (method_ === 'app_update') { + request = new devicePb.AppUpdateReq() + request.setAppName(appName) } - } -} - -function bindCheck (filePath) { - const bindCheckReq = new devicePb.BindCheckReq() - bindCheckReq.setFilePath(filePath) - const bindCheckReqBytes = bindCheckReq.serializeBinary() - const any = new proto.google.protobuf.Any() - any.setValue(bindCheckReqBytes) - const imKeyAction = new apiPb.ImkeyAction() - imKeyAction.setMethod('bind_check') - imKeyAction.setParam(any) - const imKeyActionBytes = imKeyAction.serializeBinary() - const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) - const error = callImKeyCore.getLastErrorMessage() - if (error === '' || error === null) { - const response = new devicePb.BindCheckRes.deserializeBinary(hexStr2Bytes(resBuffer)) - return { - isSuccess: true, - result: response.getBindStatus() + if (method_ === 'app_delete') { + request = new devicePb.AppDeleteReq() + request.setAppName(appName) } - } else { - const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) - return { - isSuccess: false, - result: errorResponse.getError() + const requestBytes = request.serializeBinary() + const any = new proto.google.protobuf.Any() + any.setValue(requestBytes) + const imKeyAction = new apiPb.ImkeyAction() + imKeyAction.setMethod(method_) + imKeyAction.setParam(any) + const imKeyActionBytes = imKeyAction.serializeBinary() + const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) + const error = callImKeyCore.getLastErrorMessage() + if (error === '' || error === null) { + const response = new apiPb.CommonResponse.deserializeBinary(hexStr2Bytes(resBuffer)) + return { + isSuccess: true, + result: response.getResult() + } + } else { + const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) + return { + isSuccess: false, + result: errorResponse.getError() + } } - } } -function bindAcquire (bindCode) { - const bindAcquireReq = new devicePb.BindAcquireReq() - bindAcquireReq.setBindCode(bindCode) - const bindAcquireReqBytes = bindAcquireReq.serializeBinary() - const any = new proto.google.protobuf.Any() - any.setValue(bindAcquireReqBytes) - const imKeyAction = new apiPb.ImkeyAction() - imKeyAction.setMethod('bind_acquire') - imKeyAction.setParam(any) - const imKeyActionBytes = imKeyAction.serializeBinary() - const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) - const error = callImKeyCore.getLastErrorMessage() - if (error === '' || error === null) { - const response = new devicePb.BindAcquireRes.deserializeBinary(hexStr2Bytes(resBuffer)) - return { - isSuccess: true, - result: response.getBindResult() - } - } else { - const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) - return { - isSuccess: false, - result: errorResponse.getError() +function bindCheck(filePath) { + const bindCheckReq = new devicePb.BindCheckReq() + bindCheckReq.setFilePath(filePath) + const bindCheckReqBytes = bindCheckReq.serializeBinary() + const any = new proto.google.protobuf.Any() + any.setValue(bindCheckReqBytes) + const imKeyAction = new apiPb.ImkeyAction() + imKeyAction.setMethod('bind_check') + imKeyAction.setParam(any) + const imKeyActionBytes = imKeyAction.serializeBinary() + const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) + const error = callImKeyCore.getLastErrorMessage() + if (error === '' || error === null) { + const response = new devicePb.BindCheckRes.deserializeBinary(hexStr2Bytes(resBuffer)) + return { + isSuccess: true, + result: response.getBindStatus() + } + } else { + const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) + return { + isSuccess: false, + result: errorResponse.getError() + } } - } } -export function checkUpdateAppList () { - try { - const response = getDeviceManageFunction('check_update') - const collections = response.result.availableAppListList - const list = [] - let installLoading - let installDis - let deleteDis - let deleteLoading - let buttonTexts - let version - for (let i = 0; i < collections.length; i++) { - if (collections[i].installedVersion === 'none' || collections[i].installedVersion === null) { - installLoading = false - installDis = false - deleteDis = true - deleteLoading = false - } else { - installLoading = false - installDis = true - deleteDis = false - deleteLoading = false - } - if (collections[i].latestVersion === collections[i].installedVersion) { - buttonTexts = 'install' - version = 'version ' + collections[i].installedVersion - } else { - if (collections[i].installedVersion === 'none' || collections[i].installedVersion === null) { - buttonTexts = 'install' - version = '' - } else { - buttonTexts = 'update' - version = 'version ' + collections[i].installedVersion +function bindAcquire(bindCode) { + const bindAcquireReq = new devicePb.BindAcquireReq() + bindAcquireReq.setBindCode(bindCode) + const bindAcquireReqBytes = bindAcquireReq.serializeBinary() + const any = new proto.google.protobuf.Any() + any.setValue(bindAcquireReqBytes) + const imKeyAction = new apiPb.ImkeyAction() + imKeyAction.setMethod('bind_acquire') + imKeyAction.setParam(any) + const imKeyActionBytes = imKeyAction.serializeBinary() + const resBuffer = callImKeyCore.callImKeyApi(bytes2HexStr(imKeyActionBytes)) + const error = callImKeyCore.getLastErrorMessage() + if (error === '' || error === null) { + const response = new devicePb.BindAcquireRes.deserializeBinary(hexStr2Bytes(resBuffer)) + return { + isSuccess: true, + result: response.getBindResult() + } + } else { + const errorResponse = new apiPb.ErrorResponse.deserializeBinary(hexStr2Bytes(error)) + return { + isSuccess: false, + result: errorResponse.getError() } - } - // 过滤imkey Applet BTC Applet 不能删除 - if (collections[i].appName === 'IMK' || collections[i].appName === 'BTC') { - deleteDis = true - } - const collection = { - name: collections[i].appName, - desc: version, - lastVersion: 'version ' + collections[i].latestVersion, - id: i, - installLoading: installLoading, - installDis: installDis, - deleteDis: deleteDis, - deleteLoading: deleteLoading, - icon: collections[i].appLogo, - buttonTexts: buttonTexts - } - list.push(collection) - } - const total = list.length - const status = response.result.status - return { - isSuccess: true, - result: _.cloneDeep({ status: status, total: total, list: list }) } - } catch (err) { - return { - isSuccess: false, - result: err +} + +export function checkUpdateAppList() { + try { + const response = getDeviceManageFunction('check_update') + const collections = response.result.availableAppListList + const list = [] + let installLoading + let installDis + let deleteDis + let deleteLoading + let buttonTexts + let version + for (let i = 0; i < collections.length; i++) { + if (collections[i].installedVersion === 'none' || collections[i].installedVersion === null) { + installLoading = false + installDis = false + deleteDis = true + deleteLoading = false + } else { + installLoading = false + installDis = true + deleteDis = false + deleteLoading = false + } + if (collections[i].latestVersion === collections[i].installedVersion) { + buttonTexts = 'install' + version = 'version ' + collections[i].installedVersion + } else { + if (collections[i].installedVersion === 'none' || collections[i].installedVersion === null) { + buttonTexts = 'install' + version = '' + } else { + buttonTexts = 'update' + version = 'version ' + collections[i].installedVersion + } + } + // 过滤imkey Applet BTC Applet 不能删除 + if (collections[i].appName === 'IMK' || collections[i].appName === 'BTC') { + deleteDis = true + } + const collection = { + name: collections[i].appName, + desc: version, + lastVersion: 'version ' + collections[i].latestVersion, + id: i, + installLoading: installLoading, + installDis: installDis, + deleteDis: deleteDis, + deleteLoading: deleteLoading, + icon: collections[i].appLogo, + buttonTexts: buttonTexts + } + list.push(collection) + } + const total = list.length + const status = response.result.status + return { + isSuccess: true, + result: _.cloneDeep({status: status, total: total, list: list}) + } + } catch (err) { + return { + isSuccess: false, + result: err + } } - } } -export function getSeid () { - return getDeviceManageFunction('get_seid') +export function getSeid() { + return getDeviceManageFunction('get_seid') } -export function getSn () { - return getDeviceManageFunction('get_sn') +export function getSn() { + return getDeviceManageFunction('get_sn') } -export function getRamSize () { - const response = getDeviceManageFunction('get_ram_size') - if (response.isSuccess) { - return { - isSuccess: true, - result: parseInt(response.result.substring(4, 8), 16) - } - } else { - return { - isSuccess: false, - result: response.result +export function getRamSize() { + const response = getDeviceManageFunction('get_ram_size') + if (response.isSuccess) { + return { + isSuccess: true, + result: parseInt(response.result.substring(4, 8), 16) + } + } else { + return { + isSuccess: false, + result: response.result + } } - } } -export function getFirmwareVersion () { - const response = getDeviceManageFunction('get_firmware_version') - if (response.isSuccess) { - return { - isSuccess: true, - result: response.result.substring(0, 1) + '.' + response.result.substring(1, 2) + '.' + response.result.substring(2) - } - } else { - return { - isSuccess: false, - result: response.result +export function getFirmwareVersion() { + const response = getDeviceManageFunction('get_firmware_version') + if (response.isSuccess) { + return { + isSuccess: true, + result: response.result.substring(0, 1) + '.' + response.result.substring(1, 2) + '.' + response.result.substring(2) + } + } else { + return { + isSuccess: false, + result: response.result + } } - } } -export function getSdkInfo () { - return getDeviceManageFunction('get_sdk_info') +export function getSdkInfo() { + return getDeviceManageFunction('get_sdk_info') +} + +export function activeDevice() { + return getDeviceManageFunction('device_activate') } -export function activeDevice () { - return getDeviceManageFunction('device_activate') +export function cosUpdate() { + return getDeviceManageFunction('cos_update') } -export function cosUpdate () { - return getDeviceManageFunction('cos_update') +export function cosCheckUpdate() { + return getDeviceManageFunction('cos_check_update') } -export function cosCheckUpdate () { - return getDeviceManageFunction('cos_check_update') + +export function isBLStatus() { + return getDeviceManageFunction('is_bl_status') } -export function isBLStatus () { - return getDeviceManageFunction('is_bl_status') + +export function checkDevice() { + return getDeviceManageFunction('device_secure_check') } -export function checkDevice () { - return getDeviceManageFunction('device_secure_check') +export function checkUpdate() { + return getDeviceManageFunction('check_update') } -export function checkUpdate () { - return getDeviceManageFunction('check_update') +export function downloadApplet(AppName) { + return appletManage('app_download', AppName) } -export function downloadApplet (AppName) { - return appletManage('app_download', AppName) +export function updateApplet(AppName) { + return appletManage('app_update', AppName) } -export function updateApplet (AppName) { - return appletManage('app_update', AppName) +export function deleteApplet(AppName) { + return appletManage('app_delete', AppName) } -export function deleteApplet (AppName) { - return appletManage('app_delete', AppName) +export function deviceBindCheck(filePath) { + return bindCheck(filePath) } -export function deviceBindCheck (filePath) { - return bindCheck(filePath) +export function deviceBindAcquire(bindCode) { + return bindAcquire(bindCode) } -export function deviceBindAcquire (bindCode) { - return bindAcquire(bindCode) +export function deviceBindDisplay() { + return getDeviceManageFunction('bind_display_code') } -export function deviceBindDisplay () { - return getDeviceManageFunction('bind_display_code') +export function getUserPath() { + const electron = require('electron') + const dataPath = (electron.app || electron.remote.app).getPath('userData') + '/' + return { + isSuccess: true, + result: dataPath + } } -export function getUserPath () { - const electron = require('electron') - const dataPath = (electron.app || electron.remote.app).getPath('userData') + '/' - return { - isSuccess: true, - result: dataPath - } +export function importBindCode(bindCode) { + const response = getUserPath(); + if (response.isSuccess) { + //存储路径 + const bindCodePath = response.result + "bindCode.json" + //加密绑定码 + const enBindCode ={ + bindCode:crypto.encryptData(bindCode, "8c5fab512604d93f836afd5071ac0688") + } + // fs.writeFile 写入文件(会覆盖之前的内容)(文件不存在就创建) utf8参数可以省略 + try { + fs.writeFileSync(bindCodePath, JSON.stringify(enBindCode), 'utf8') + return { + isSuccess: true, + } + } catch (error) { + return { + isSuccess: false, + result: error + } + } + } +} + +export function exportBindCode() { + const response = getUserPath(); + if (response.isSuccess) { + //存储路径 + const bindCodePath = response.result + "bindCode.json" + try { + const data = fs.readFileSync(bindCodePath, 'utf-8') + const dataString = data.toString()// 将二进制的数据转换为字符串 + const jsonObj = JSON.parse(dataString)// 将字符串转换为json对象 + return { + isSuccess: true, + //解密绑定码 + result: crypto.decryptData(jsonObj.bindCode, "8c5fab512604d93f836afd5071ac0688") + } + } catch (error) { + return { + isSuccess: false, + result: error + } + } + } } @@ -363,16 +414,16 @@ export function getUserPath () { * @param arr * @returns {string} */ -function bytes2HexStr (arr) { - let str = '' - for (let i = 0; i < arr.length; i++) { - let tmp = arr[i].toString(16) - if (tmp.length === 1) { - tmp = '0' + tmp +function bytes2HexStr(arr) { + let str = '' + for (let i = 0; i < arr.length; i++) { + let tmp = arr[i].toString(16) + if (tmp.length === 1) { + tmp = '0' + tmp + } + str += tmp } - str += tmp - } - return str + return str } /** @@ -380,19 +431,19 @@ function bytes2HexStr (arr) { * @param str * @returns {any[]|null} */ -function hexStr2Bytes (str) { - let pos = 0 - let len = str.length - if (len % 2 !== 0) { - return null - } - len /= 2 - const hexA = new Array() - for (let i = 0; i < len; i++) { - const s = str.substr(pos, 2) - const v = parseInt(s, 16) - hexA.push(v) - pos += 2 - } - return hexA +function hexStr2Bytes(str) { + let pos = 0 + let len = str.length + if (len % 2 !== 0) { + return null + } + len /= 2 + const hexA = new Array() + for (let i = 0; i < len; i++) { + const s = str.substr(pos, 2) + const v = parseInt(s, 16) + hexA.push(v) + pos += 2 + } + return hexA } diff --git a/src/main/index.js b/src/main/index.js index e292498..6cc46cc 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -503,6 +503,14 @@ function renderDeviceManagerHandler () { const response = deviceManger.getUserPath() mainWindow.webContents.send('getUserPathResult', response) }) + ipcMain.on('importBindCode', (event, bindCode) => { + const response = deviceManger.importBindCode(bindCode) + mainWindow.webContents.send('importBindCodeResult', response) + }) + ipcMain.on('exportBindCode', () => { + const response = deviceManger.exportBindCode() + mainWindow.webContents.send('exportBindCodeResult', response) + }) } /** From d4947269950344296537b656da4d52f23ba31564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cfengweiqiang-coder=E2=80=9D?= <“weiqiang@imkey.im”> Date: Fri, 3 Jul 2020 15:51:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8dotenv=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=9A=84key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 1 + package.json | 1 + src/api/devicemanagerapi.js | 4 ++-- src/main/index.js | 3 ++- 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..cf5d229 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +bindCode_encryptionKey=8c5fab512604d93f836afd5071ac0688 \ No newline at end of file diff --git a/package.json b/package.json index fbe4c72..1d7a972 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "@sentry/electron": "^1.3.0", "body-parser": "^1.19.0", "cors": "^2.8.5", + "dotenv": "^8.2.0", "electron-updater": "^4.3.1", "element-ui": "^2.13.1", "express": "^4.17.1", diff --git a/src/api/devicemanagerapi.js b/src/api/devicemanagerapi.js index ae63ad6..0a27dc1 100644 --- a/src/api/devicemanagerapi.js +++ b/src/api/devicemanagerapi.js @@ -347,7 +347,7 @@ export function importBindCode(bindCode) { const bindCodePath = response.result + "bindCode.json" //加密绑定码 const enBindCode ={ - bindCode:crypto.encryptData(bindCode, "8c5fab512604d93f836afd5071ac0688") + bindCode:crypto.encryptData(bindCode, process.env.bindCode_encryptionKey) } // fs.writeFile 写入文件(会覆盖之前的内容)(文件不存在就创建) utf8参数可以省略 try { @@ -376,7 +376,7 @@ export function exportBindCode() { return { isSuccess: true, //解密绑定码 - result: crypto.decryptData(jsonObj.bindCode, "8c5fab512604d93f836afd5071ac0688") + result: crypto.decryptData(jsonObj.bindCode, process.env.bindCode_encryptionKey) } } catch (error) { return { diff --git a/src/main/index.js b/src/main/index.js index 6cc46cc..7886b0c 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -8,7 +8,8 @@ import pkg from '../../package.json' const deviceManger = require('../api/devicemanagerapi') const walletApi = require('../api/walletapi') - +// 默认读取项目根目录下的.env文件 +require('dotenv').config() /** * Set `__static` path to static files in production * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html