std :: string_view编译时哈希 [英] std::string_view compile time hashing

查看:152
本文介绍了std :: string_view编译时哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C +,似乎 std :: hash函数 +17 string_view不是constexpr.

It seems the std::hash functions for the C++17 string_view are not constexpr's.

在我看来,绑定到const char []的字符串视图可以在编译时进行哈希处理(这非常好用),或者有什么方法可以防止这种情况发生?

It seems to me that a string view bound to a const char[] could be hashed at compile time (which would be very sweet), or is there anything which prevents this?

推荐答案

自C ++ 14(请参见17.6.3.4哈希要求,表26)以来,我们具有:

Since C++14 (see 17.6.3.4 Hash requirements, table 26), we have:

在持续时间内返回的值应仅取决于参数k 该程序. [注意:因此,对表达式h(k)的所有求和 对于k的给定执行,相同的k值会产生相同的结果 程序. -尾注]

The value returned shall depend only on the argument k for the duration of the program. [Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result for a given execution of the program. -- end note]

两个不同的执行可以给出不同的哈希值:

仅需要哈希函数才能产生相同的结果 在程序的单次执行中相同的输入; 这允许防止冲突DoS攻击的盐渍哈希.

Hash functions are only required to produce the same result for the same input within a single execution of a program; this allows salted hashes that prevent collision DoS attacks.

此行为很有用,可缓解基于哈希冲突的DoS攻击.

以下是有关C ++ 17标准中Hash概念要求的措辞:

Here's the wording about the requirements of the Hash concept from the C++17 standard:

返回的值应仅取决于参数k 在计划期间. [注意:所有 表达式h(k)的计算结果与 k的值产生相同的结果 程序的给定执行. —尾注]

The value returned shall depend only on the argument k for the duration of the program. [ Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result for a given execution of the program. — end note ]

它没有明确声明有关随机哈希的任何内容. std::hash文本不具有强制性,也不排除随机散列.

It does not explicitly state anything about random hashing. The std::hash text does not mandate and does not preclude random hashing.

这是 N3242 2011年2月28日未提及在计划实施期间"的草案:

Here's the N3242 2011-02-28 draft which did not mention "for the duration of the program":

不得引发异常.返回的值应仅取决于 关于论点k. [注意:因此,对表达式h(k)的所有求值 相同的k值会产生相同的结果. —尾注

Shall not throw exceptions. The value returned shall dependonly on the argument k. [Note: thus all evaluations of the expression h(k) with the same value for k yield the same result. — end note

我们可以看到,添加了在程序运行期间"为给定的程序执行时间"作为"

We can see that the addition of "for the duration of the program" "for a given execution of the program" was added as a resolution for "2291. std::hash is vulnerable to collision DoS attack".

AFAIU,std::hash的任何实现都不能实现随机哈希,但是您可以编写自己的my::secure_hash.

AFAIU, no implementation of std::hash implements random hashing but you can write your own my::secure_hash.

这篇关于std :: string_view编译时哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆