我正在尝试导出一个模块,该模块应该存储给定信息的哈希表,以便可以检查另一个访问该信息的调用是否存在于哈希表中,如果找到,则返回哈希表中的值.
我无法在导出中获取哈希表以在整个应用程序中保持一致,作为单例/静态/全局变量.
这就是我所拥有的:
var Randomize = {
hashTable: [],
randomize: function(rows) {
var randomized = [];
for(var i in rows) {
//check if exists in hashtable, use values accordingly
}
return randomized;
}
};
module.exports = Randomize;
Run Code Online (Sandbox Code Playgroud)
当我尝试访问它时:
var randomize = require('randomize');
/* ... */
console.log(randomize.randomize(rows))
Run Code Online (Sandbox Code Playgroud)
它为每个实例创建一个新的哈希表.我怎样才能使它重用哈希表的相同实例?
我正在尝试在Golang中实现HOTP(rfc-4226),我正在努力生成有效的HOTP.我可以在java中生成它但由于某种原因我在Golang中的实现是不同的.以下是样本:
public static String constructOTP(final Long counter, final String key)
throws NoSuchAlgorithmException, DecoderException, InvalidKeyException {
final Mac mac = Mac.getInstance("HmacSHA512");
final byte[] binaryKey = Hex.decodeHex(key.toCharArray());
mac.init(new SecretKeySpec(binaryKey, "HmacSHA512"));
final byte[] b = ByteBuffer.allocate(8).putLong(counter).array();
byte[] computedOtp = mac.doFinal(b);
return new String(Hex.encodeHex(computedOtp));
}
Run Code Online (Sandbox Code Playgroud)
在Go:
func getOTP(counter uint64, key string) string {
str, err := hex.DecodeString(key)
if err != nil {
panic(err)
}
h := hmac.New(sha512.New, str)
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, counter)
h.Write(bs)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于Java行:ByteBuffer.allocate(8).putLong(counter).array();
生成与Go行不同的字节数组:binary.BigEndian.PutUint64(bs, counter)
. …
mkfifo()
尝试进入当前目录时出现权限错误。我绝对有权在这里创建文件。知道问题出在哪里吗?
char dir[FILENAME_MAX];
getcwd(dir, sizeof(dir));
for(i = 0; i<num_nodes; i++)
{
char path[FILENAME_MAX];
sprintf(path, "%s/%d",dir, i);
printf("%s\n", path);
fifoArray[i] = mkfifo(path, O_WRONLY);
if(fifoArray[i] < 0)
{
printf("Couldn't create fifo\n");
perror(NULL);
}
}
Run Code Online (Sandbox Code Playgroud) c ×1
cryptography ×1
express ×1
fifo ×1
go ×1
java ×1
javascript ×1
mkfifo ×1
named-pipes ×1
node.js ×1
sha512 ×1