小编Ken*_* Ki的帖子

Javascript:配置模式

问题:Javascript函数需要很少的参数来处理:

function kick(person, reason, amount) {
    // kick the *person* with the *amount*, based on the *reason*
}
Run Code Online (Sandbox Code Playgroud)

由于没有办法像JS中那样在JS中执行函数重载,如果它需要设计为便于将来改进(参数添加),它可以写成:

/* Function Parameters pattern */
function kick() {
    // kick the person as in *arguments[0]*, with the amount as in *arguments[1]*,
    // based on the reason as in *arguments[2]*, with the strength as in *arguments[3]*
}
Run Code Online (Sandbox Code Playgroud)

要么

/* Object Configuration Pattern */
function kick(config) {
    // kick the person as in *config.person*, with the amount as in *config.amount*, …
Run Code Online (Sandbox Code Playgroud)

javascript design-patterns coding-style

5
推荐指数
1
解决办法
7310
查看次数

使用 ECB 模式的 CryptoJS AES 加密产生具有相同参数的不同结果

如本答案所述,我可以使用 ECB 模式将转换后的值反转回明文,而不仅仅是将其与另一个散列值进行比较。

但是,使用以下代码片段:

const x = CryptoJS.AES.encrypt('abc', '123', { mode: CryptoJS.mode.ECB }).toString()
const y = CryptoJS.AES.encrypt('abc', '123', { mode: CryptoJS.mode.ECB }).toString()

console.log(x, y, x === y)
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

我得到:

U2FsdGVkX19blKXDRXfdgXyviCrZtouB0cPcJPoR/cQ= U2FsdGVkX1+1AwWqKWntLVkh7DtiZxPDYCDNsjmc8LM= false
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?有没有办法达到预期的结果?

javascript cryptojs

3
推荐指数
1
解决办法
2473
查看次数