Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我不会太担心生成令人难以置信的强一次性密码。把密码设置得长一些,如果你限制了密码的有效时间,那么暴力破解应该不会有问题。如果密码的有效期只有 1 小时,那么密码一直不使用也不会出现问题。在这段时间内,不太可能有人用暴力破解它。
同样重要的是,您只能让一次性密码只能使用一次。这样,如果密码被拦截,用户将注意到一次性密码何时过期,并可以采取适当的操作。
我会选择 Apache Commons RandomStringUtils 并让密码为 10-15 个字母和数字字符。
...尽管这始终是一个你想变得多么偏执的问题。这个解决方案对于常规的 Web 应用程序来说很好,但对于银行来说还不够好......
I would not worry that much about generating incredible strong one time passwords. Make the password long and it should not be a problem with brute force granted you limit how long the password is valid. If the password is only valid for say 1 hour then it will not be a problem if the password remains unused. And in that time span it is not likely that someone will get to crack it using brute force.
It is also important that you only let the one time password work just one time. This way, if the password is intercepted the user will notice when the one time password has expired and can take appropriate actions.
I'd go for Apache Commons RandomStringUtils and let the password be 10-15 characters of letters and numbers.
...though it always is a question of how paranoid you want to be. This solution would be fine for a regular web application, but not good enough for a bank...
它位于 .net 中,但转换起来应该很简单。对于大多数人来说可能有点太多了,但这是我在我的应用程序中经常使用的实现方式。这是我前段时间发现的一个实现,并做了一些修改,我不记得原作者了,但我会快速搜索一下,看看是否可以给予他适当的认可。
编辑
我相信我从发布的代码开始
http://www.obviex.com/Samples/Password.aspx。
尽管该代码现在具有更多功能。
It's in .net but should be trivial to convert. Maybe a little too much for most, but this is the go to implementation that I always use in my apps. It is an implementation that I found some time ago, and did some modifications to, I can't recall the original author, but I will do a quick search to see if I can give him appropriate credit.
Edit
I believe I started with the code posted at
http://www.obviex.com/Samples/Password.aspx.
Though the code now has a few more features.
这是一个使用 Commons 的示例。它创建一个长度为 8 到 20 个字符的字母数字密码。
更新
RandomUtils.nextInt 返回 0(含)和指定值(不含)之间的数字,因此要获取 8 到 20 个字符(含)之间的值,参数值应为 13。我已更正了上面的代码。
更新
正如下面的评论中指出的,这可以在不使用 StringBuffer 的情况下编写。这是修改后的一行版本:
Here is an example using Commons. It creates an Alphanumeric password between 8 and 20 characters long.
UPDATE
RandomUtils.nextInt returns a number between 0 (inclusive) and the specified value (exclusive) so to get a value between 8 and 20 characters inclusive, the argument value should be 13. I've corrected the code above.
UPDATE
As noted in a comment below, this could be written without using StringBuffer. Here is a modified one line version:
对于那些感兴趣的人,这里是 Matthew 的代码,转换为 Java
和单元测试
For those interested, here's Matthew's code, converted to Java
And a unit test
Password Safe 是开源的(根据 Artistic 许可证)并包含密码生成代码。
Password Safe is open source (under the Artistic License) and includes password generation code.
我添加了一个与 C#/Java 版本类似的 Golang 实现。它在 Apache 2.0 下可用。来源位于此处:
https://github.com/deftlabs/dlshared/blob /master/password_utils.go
I added a Golang implementation that is similar to the C#/Java versions. It is available under Apache 2.0. The source is located here:
https://github.com/deftlabs/dlshared/blob/master/password_utils.go
您可以使用 Random 和内置的 MessageDigest 实现轻松实现它。
You can easily implement it using Random and the built in MessageDigest implementations.