【发布时间】:2020-11-02 17:48:36
【问题描述】:
我已经做了 Java 和 Kotlin 很多年了。但由于某种原因,我无法理解这些 JS 承诺链...
在过去的 2 个小时里,我一直试图弄清楚如何从这个 AES 示例代码中返回加密的字符串,或者从这样的链中返回任何其他内容。
import { NativeModules, Platform } from 'react-native'
const Encrypt = (message) => {
const Aes = NativeModules.Aes
const options = {
password: "Password",
salt: "salt",
cost: 5000,
length: 256
}
const GenerateKey = (password, salt, cost, length) => {
Aes.pbkdf2(
password || options.password,
salt || options.salt,
cost || options.cost,
length || options.length
)
}
const DecryptData = (cipher, iv, key) => Aes.decrypt(cipher, key, iv)
const EncryptData = (text, key) => {
return Aes.randomKey(16).then(iv => {
return Aes.encrypt(text, key, iv).then(cipher => ({
cipher,
iv
}))
})
}
const example = () => {
generateKey('asd', 'salt', 2000, 256)
.then( key => {
console.log('Key:', key)
encryptData(message, key)
.then(({ cipher, iv }) => {
console.log('Encrypted:', cipher)
decryptData(cipher, iv, key)
.then(text => {
console.log('Decrypted:', text)
})
.catch(error => {
console.log(error)
})
Aes.hmac256(cipher, key).then(hash => {
console.log('HMAC', hash)
})
})
.catch(error => {
console.log(error)
})
})
}
}
export default Encrypt
所有控制台日志都会触发,如果我取消示例周围的函数声明,那么代码应该可以工作。
问题 1:我如何只从链中返回密码?
问题 2:我能以某种方式只导出 EncryptData 函数,单独使用吗?
问题 3:有没有人有任何资源,比如让你理解这个流程的教程?
我已经用谷歌搜索并阅读了几个教程,但 JS 语法似乎每隔一天就会改变一次,而且似乎没有一个教程是关于这种链的..
谢谢
【问题讨论】:
标签: react-native promise