如何使用当前时间戳创建随机字符串作为Java中的salt? [英] How to create a random String using the current timestamp as the salt in Java?

查看:293
本文介绍了如何使用当前时间戳创建随机字符串作为Java中的salt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据当前时间戳创建一个随机字符串(输出到控制台以进行调试)。

I would like to create a random string (to output to the console for debugging purposes) based off of the current timestamp.

例如,控制台会输出:

Setting up browser [123456]...
Getting configuration [758493]...
Completed: [758493].
Completed: [123456].

这里 123456 758493 是我试图生成的随机字符串。

Here the 123456 and 758493 are the random strings I'm trying to generate.

这是我认为它可以工作的伪代码:

Here's the pseudocode for how I think it can work:

private String random(int len){
long ts = getCurrentTimestamp;
String value = createRandom(len, ts); 
    //len is the length of the randomString
    //and ts is the salt
return value;
}

任何人都可以帮忙解决此问题(需要导入的内容),和/或可能建议对此进行改进?

Can anyone help with the details of this (what needs to be imported), and/or possibly suggest improvements to this?

推荐答案

这取决于当前时间戳的含义。您可以使用 System.currentTimeMillis() ,但不一定是唯一 - 如果你在短时间内多次调用它,你可能会得到几次相同的结果。还有 System.nanoTime()

Well it depends on what you mean by "current timestamp". You could use System.currentTimeMillis(), but that won't necessarily be unique - if you call it several times in a short period, you may well get the same results several times. There's also System.nanoTime().

作为替代方案,您可以使用 UUID.randomUUID() ,使用所有位或某些子集。 (如果您决定使用子集,则应仔细选择它们。并非UUID中的所有位都相同。)

As an alternative, you could use UUID.randomUUID(), either using all the bits or some subset. (If you decide to use a subset, you should choose them carefully. Not all bits in a UUID are equal.)

这篇关于如何使用当前时间戳创建随机字符串作为Java中的salt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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