Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random String in linux by system time

Tags:

linux

random

I work with Bash. I want to generate randrom string by system time . The length of the unique string must be between 10 and 30 characters.Can anybody help me?

like image 803
mindia Avatar asked Jul 15 '13 09:07

mindia


People also ask

How do you generate random strings?

A random string is generated by first generating a stream of random numbers of ASCII values for 0-9, a-z and A-Z characters. All the generated integer values are then converted into their corresponding characters which are then appended to a StringBuffer.

How to create a random string in bash?

The very first method we can use to generate a random string in bash is md5 checksums. Bash has the $RANDOM variable, which produces a random number. We can pipe this value to md5sum to get a random string. The $RANDOM variable is always random.

Is there a random string in python?

The random module in python is used to generate random strings. The random string is consisting of numbers, characters and punctuation series that can contain any pattern. The random module contains two methods random. choice() and secrets.


2 Answers

Maybe you can use uuidgen -t.

Generate a time-based UUID. This method creates a UUID based on the system clock plus the system's ethernet hardware address, if present.

like image 20
Thilo Avatar answered Sep 28 '22 10:09

Thilo


There are many ways to do this, my favorite one using the urandom device:

burhan@sandbox:~$ tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
CCI4zgDQ0SoBfAp9k0XeuISJo9uJMt
  • tr (translate) makes sure that only alphanumerics are shown
  • fold will wrap it to 30 character width
  • head makes sure we get only the first line

To use the current system time (as you have this specific requirement):

burhan@sandbox:~$ date +%s | sha256sum | base64 | head -c30; echo
NDc0NGQxZDQ4MWNiNzBjY2EyNGFlOW
  • date +%s = this is our date based seed
  • We run it through a few hashes to get a "random" string
  • Finally we truncate it to 30 characters

Other ways (including the two I listed above) are available at this page and others if you simply google.

like image 120
Burhan Khalid Avatar answered Sep 28 '22 10:09

Burhan Khalid