Creating Random Strings: Hash Functions & Beyond

In summary, a hash function takes a string as an argument and outputs a string of a smaller size which is a hash that represents the original string. A random byte generator is used to produce a random string. If you want to produce a random string not what a hash function does, you need a random byte generator.
  • #1
chaoseverlasting
1,050
3
What exactly are hash functions and how do you use them to create a random string? What other ways are there to create random strings.
 
Technology news on Phys.org
  • #2
http://en.wikipedia.org/wiki/Hash_functions

One does not implement hash functions to produce a random string. Hash functions take a string as an argument and output a string of a smaller size which is a hash that represents the original string. An example of a hash function would be the famous DES.

If you wish to produce a random string and not what a hash function does, you need a random byte generator. There are many methods of random generators. A common implementation of a random generator is to take the value of the time, and do a manipulation on it. Once you have a random byte generator, you can produce the number of bytes you want to produce a random string.
 
Last edited:
  • #3
How would you get the system time in c++ and use it to create a random string?
 
  • #4
I am not a C programmer but it seems to me you'd use the pseudo random number generator and seed it with the time:



Code:
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
srand(time(0));
 
  • #5
chaoseverlasting said:
How would you get the system time in c++ and use it to create a random string?

Here is an example of a random string generator written in C. It generates a string of random capital letters.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(int argc, char *argv[])
{
        if(argc < 2){
                printf("USAGE: %s [Length of string]\n", argv[0]);
                return 0;
        }

        int length      = atoi(argv[1]);
        char *randstr   = malloc(length + 1);
        randstr[length] = '\0';

        time_t ltime = time(NULL);
        srand(ltime);

        int i;
        for(i = 0; i < length; i++)
                randstr[i] = (rand() % 26) + 65;

        printf("%s\n", randstr);
        return 0;
}

Here is an example of the output:

Code:
$ ./randstr
USAGE: ./randstr [Length of string]
$ ./randstr 20
MTZEKHHTQJGPDNHBNSTN
$ ./randstr 20
IEMBTDVFDEDMFWXEYESX
$ ./randstr 20
EMCYCCJOTCAJHCNHMQOH
$
 
Last edited:

1. What is a hash function and how does it create random strings?

A hash function is a mathematical algorithm that takes in an input of any size and produces an output of fixed size. It is designed to be a one-way function, meaning it is easy to compute the output from the input, but difficult to determine the input from the output. This makes it useful for creating random strings as the output appears to be random and is difficult to predict.

2. How are hash functions used in the creation of random strings?

Hash functions are used in the creation of random strings by taking in a specific input (such as a password) and generating a unique output (hash). This hash can then be used as a random string as it is difficult to predict and cannot be easily reversed to determine the input. This makes it useful for generating secure passwords and other random strings.

3. Can a hash function generate the same output for two different inputs?

Yes, it is possible for a hash function to generate the same output for two different inputs. This is known as a hash collision and can occur due to the fixed size of the output. However, a good hash function should have a low probability of producing a collision, making it a reliable method for creating random strings.

4. Are there other methods besides hash functions for creating random strings?

Yes, there are other methods for creating random strings, such as using random number generators or cryptographic algorithms. However, hash functions are a popular choice as they are efficient, easy to implement, and have a low probability of collisions.

5. Are there any limitations to using hash functions for creating random strings?

One limitation of using hash functions for creating random strings is the fixed size of the output. This means that the generated strings may not always be of the desired length. Additionally, if the input is known or can be easily guessed, it may be possible to determine the output. Therefore, it is important to use a strong and secure hash function for creating random strings.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
816
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
21
Views
3K
  • Programming and Computer Science
Replies
2
Views
633
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
638
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
9
Views
906
Back
Top