gpt4 book ai didi

javascript - JS中如何创建私有(private)变量

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:20 25 4
gpt4 key购买 nike

我想在需要文件/模块时保存私有(private)变量( secret )。 secret 应保存在文件 sec_test.js 的“对象”中,并且它不应该可读或可写,只能执行。这是正确的方法吗?

问题1:是否有可能在执行过程中以某种方式从testing_sec_test.js 获取 secret ?

问题2:sec_test.js 中是否可以有一个类似构造函数的函数?

文件:sec_test.js

module.exports = function (string) {
var module = {};
let secret = null;


module.get_secret_length = function (callback) {
generate_secret();
if(secret == null){
const json_err = {
"Success":false,
"error":"generating secret failed"
};
callback(json_err,null);
}else{
const json_err = {
"Success":true,
"result":"secret has been generated",
"secret_length":get_secret_length()
};
callback(json_err,null);
}
}

function generate_secret(){
if(secret == null){
secret = getRandomString()+string+getRandomString();
}
}

function get_secret_length(){
return secret.length;
}

function getRandomString(){
const length = Math.floor(Math.random() * Math.floor(200));
const characters_allowed = '@1#2$3&/=?:.;,+_-><~*^|4567890'+
'qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM';
let random_string = "";
for(let i =0;i<length;i++){
let random_nbr = Math.floor(Math.random() * Math.floor(characters_allowed.length));
random_string += characters_allowed.charAt(random_nbr);
}
return random_string;
}


return module;
};

文件:testing_sec_test.js

const sec_test = require('./sec_test')("IS THIS SECRET A PRIVATE VARIABLE");

console.log(sec_test.get_secret_length.toString());

sec_test.get_secret_length(function(err,result){
if(err){
console.log(err);
}else{
console.log(result);
}
});

----------------------------------------------------------------------------------------

<小时/>

我想我必须更好地表达我的问题,抱歉

问题1:获取对象并输入参数后是否可以获取key或ivKey?或者这个对象使用起来不安全,因为它的 key 或 ivKey 是公共(public)可访问的?

<小时/>

文件:testing_sec_test.js

//lets pretend that these keys is written in from the terminal to the object and are NOT hardcoded in the code!.
let sec_Obj = {
"key": '1234zr3p67VC61jmV54rIYu1545x4TlY',
"ivKey": "123460iP0h6vJoEa",
"salt": "1kg8kfjfd2js93zg7sdg485sd74g63d2",
"key_iterations": 87923
}

const sec_test = require('./sec_test')(sec_Obj);
sec_Obj = null;

console.log(sec_test);
let plain_text = "This is a national secret";
console.log("plain_text == "+plain_text);
sec_test.encrypt(plain_text,function(err,encrypted){
if(err){
console.log(err);
}else{
console.log("encrypted == "+encrypted);
sec_test.decrypt(encrypted,function(err,decrypted){
if(err){
console.log(err);
}else{
console.log("decrypted == "+decrypted);
}
});

}
});
<小时/>

文件:sec_test.js

const crypto = require('crypto');
module.exports = function (keysObj) {
//is the parameter keysObj private??
var module = {};

module.encrypt = function (clearData,callback) {
let str_encoding = "utf8";
let encoding = "base64";
try {
let encipher = crypto.createCipheriv('aes-256-ctr', getPrivateKey(), getPrivateIvKey());
let result = encipher.update(clearData, str_encoding, encoding);
result += encipher.final(encoding);
callback(null,result);
} catch (error) {
callback({"success":false,"error":error},null);
}
}

module.decrypt = function(encrypted,callback) {
let str_encoding = "utf8";
let encoding = "base64";
try {
let decipher = crypto.createDecipheriv('aes-256-ctr',getPrivateKey(), getPrivateIvKey());
let result = decipher.update(encrypted, encoding, str_encoding);
result += decipher.final(str_encoding);
callback(null,result);
} catch (error) {
callback({"success":false,"error":error},null);
}
}

//is this a private function
function getPrivateKey(){
return crypto.pbkdf2Sync(keysObj['key'], keysObj['salt'], keysObj['key_iterations'], 32, 'sha512');
}

//is this a private function
function getPrivateIvKey(){
return new Buffer(keysObj['ivKey']);
}

return module;
};

最佳答案

简单的例子

var privateVar = 'private';


module.exports = {

test:function(){
console.log('I am '+privateVar);
}
}

var test = require('./test.js');

//logs i am private
test.test()
//logs undefined
test.privateVar

关于javascript - JS中如何创建私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com