Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cbd2a84

Browse files
author
zhupeiquan
committed
How_to_generate_a_random_alpha-numeric_string.md 修正
1 parent a0b92fe commit cbd2a84

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

‎contents/How_to_generate_a_random_alpha-numeric_string.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public class RandomString {
3333
}
3434
```
3535

36-
为了安全,可以考虑使用下面这段简洁且安全的代码,不过用其作为 session 的标识符,可能略显昂贵了一点:
36+
为了安全,可以考虑使用下面这段简洁且安全的代码,不过用其作为 session 的标识符,倒显得有点大材小用了:
3737
```
38-
```
38+
import java.security.SecureRandom;
39+
40+
public final class SessionIdentifierGenerator {
41+
private SecureRandom random = new SecureRandom();
42+
43+
public String nextSessionId() {
44+
return new BigInteger(130, random).toString(32);
45+
}
46+
}
47+
```
48+
49+
其工作原理就是,使用一个 130 位的安全的随机数生成器生成一个随机数,接着转化为 32 进制。我们知道,128 位安全随机数的生成已经是足够安全的,不过以 32 进制编码的每一个数字可编码 5 位,所以需要取大于 128 且是 5 的倍数,所以就选择了 130 位。相对于 随机 UUID 来说(在标准输出中,每个字符使用 3.4 bit,共 122 bit),每个字符使用 5 个随机的 bit 来编码的方式,显得更为简洁和高效。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /