如何解决NodeJs的BCrypt包的hash()和hashSync()函数的区别
struct ExperimentPickerView: View {
@StateObject var localExperiment = RunningExperiment.active
@StateObject var experiments = RemoteExperiments.instance
@State var picked : Int = -1
var body: some View {
Picker("active",selection: $picked) {
ForEach(Array(experiments.experiments.enumerated()),id: \.offset) { i,experiment in
Section(header: Text("\(experiment.name)")) {
ForEach(Array(experiment.variations.enumerated()),id: \.offset) { j,variation in
// Text("\(variation.name) \(variation.id)").tag(variation.id)
Text("\(variation.name) \(variation.id)").tag(UUID().description)
}
}
}
}.id(picked).onReceive([self.picked].publisher.first()) { (value) in
print(value) // prints the row number and not the id of the element
}
}
}
struct Experiment : Codable,Equatable,Identifiable {
var id: Int {
var hasher = Hasher()
name.hash(into: &hasher)
variations.hash(into: &hasher)
return hasher.finalize()
}
let name : String
let variations: [Variation]
enum CodingKeys: String,CodingKey {
case name
case variations
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let n = try container.decode(String.self,forKey: .name)
name = n
let a = try container.decode(AnyCodable.self,forKey: .variations)
print(a)
let b = a.value as! [[String:Any]]
var vars = [Variation]()
for v in b {
if let na = v["name"],let nna = na as? String {
vars.append(Variation(name: nna,experiment: n))
}
}
variations = vars
}
}
struct Variation: Codable,Identifiable,Hashable {
var id: Int {
var hasher = Hasher()
name.hash(into: &hasher)
experiment.hash(into: &hasher)
let hashValue = hasher.finalize()
return hashValue
}
let name: String
var experiment: String
enum CodingKeys: String,CodingKey {
case name,experiment
}
}
它们可能在哪些方面有所不同,它们可以互换使用吗? (非常欢迎详细解释,不胜感激!)
解决方法
hashSync 用于同步生成给定字符串的哈希值。它返回散列的字符串
hash 用于为给定字符串异步生成哈希。它返回承诺是回调已提交,您需要解决承诺。
参考https://www.npmjs.com/package/bcryptjs#hashsyncs-salt
,bcrypt.hash 将回调作为其第三个参数,当哈希完成时将调用该回调。 bcrypt.hashSync 运行散列,等待它完成并返回散列值。
换句话说,“hash”是异步的,hashSync 是同步的。
,你的意思
const bcrypt = require('bcrypt')
const hash = bcrypt.hash(<myPassword>,12) // this returns a promise
const hashSync = bcrypt.hashSync(<myPasword>,12) //this on is sync so it stops every line of code after untill it's executed
阅读this文章了解同步和异步的区别
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。