有没有使用Node js在DB中搜索加密数据的最好方法?
我将加密数据存储在数据库中。如何通过Code搜索加密文本?
示例:
$ node encrypt.js 123 'hello my friend'
90cbf635540412a202eb46dada1fcf
$ node encrypt.js 123 'hello'
90cbf63554
$ node encrypt.js 123 ' my '
d8c3e379
$ node encrypt.js 123 'friend'
9edcf33c5540发布于 2017-11-05 03:53:02
你不能搜索加密的文本--这会违背加密它的初衷。正如你注意到的,字符串的不同部分将加密成完全不同的值-这是故意的,否则它将使你的加密更容易被破解。
您可以做的是对要搜索的文本片段进行散列,并将这些散列存储在一个不同的表引用中,以返回到父级。
parent (the encrypted strings)表
id string
1 xyzxyzxyzxyzxyzxyz (encrypted 'hello my friend')然后将string分解为word组件。
var words = string.split(' '); // words is an array of ['hello', 'my', 'friend']迭代此数组并计算每个words的散列,然后将这些散列插入“索引”表中。
words 表(索引字散列到加密父对象)
id parent_id hash
1 1 abc (hash of "hello")
2 1 def (hash of "my")
3 1 ghi (hash of "friend") 此时,您将能够将搜索查询组合成不同的单词,计算它们的散列,然后返回任何匹配的parent_id。请注意,这只适用于完整的单词-任何变体都会更改计算的哈希,因此您可能希望强制所有内容都是小写的。
假设您正在搜索"hello",您将计算其散列"abc“,然后运行以下SQL查询。
SELECT p.id, p.string
FROM parent p
JOIN words w ON p.id = w.parent_id
WHERE w.hash IN ('abc')
GROUP BY p.idhttps://stackoverflow.com/questions/47114566
复制相似问题