0

What is the way to check if string is 'random generated', are there any libs for this? For example:

random_string1 = 'gr24gF3fds52'
random_string2 = '111111gr22124gF3fd55425hdhgdhgdhd4565553fd5s52'

This would be a 'normal' string:

normal1 = 'this is just a string'
normal2 = 'helo-how-are-you'
normal3 = '452415422542'

So random generated string should be string that does not have any order, it has numbers and chars. It looks like its encrypted.

taga
  • 3,537
  • 13
  • 53
  • 119
  • 1
    Why do you need know if a string is "random?" What are you going to use this info for? What would you consider a "non-random" string? – gen_Eric Feb 25 '21 at 21:58
  • 1
    There isn't. What you're looking for is Base64 Encoded. Try to Decode it, and if it fails then it's not encoded. https://stackoverflow.com/questions/12315398/check-if-a-string-is-encoded-in-base64-using-python – thethiny Feb 25 '21 at 21:58
  • 2
    I don't think you want a lib for this - you just need to establish what random means for you and see if those rules match your stiring. No "standard method" or package is going to be able to implement your rules! – michjnich Feb 25 '21 at 21:58
  • 2
    Encrypted strings wouldn't be random. – Kraigolas Feb 25 '21 at 21:58
  • 1
    There are mathematical metrics such as Shannon entropy. – AKX Feb 25 '21 at 21:58
  • I guess you can check if the string has numbers in it, and if it has an uppercase letter somewhere in the middle. – gen_Eric Feb 25 '21 at 21:58
  • 1
    Ah, is what @thethiny mentions what you want? Just to verify it's not an encoded string? – michjnich Feb 25 '21 at 21:58
  • 1
    I would also recommend that you see this https://stackoverflow.com/questions/32234169/sha1-string-regex-for-python – thethiny Feb 25 '21 at 21:58
  • Does this answer your question? [SHA1 string regex for python](https://stackoverflow.com/questions/32234169/sha1-string-regex-for-python) – Joe Ferndz Feb 25 '21 at 22:11

1 Answers1

1

The concept of "Random" is different depending on what your criteria is. But if I understand you correctly, you want to check if it's encrypted/encoded/hashed. For this I would recommend these two reads:

You can modify the 2nd one's 1st response to not include the length of 40 limit.

def is_sha1(maybe_sha):
    try:
        sha_int = int(maybe_sha, 16)
    except ValueError:
        return False
    return True

Or the 2nd response's 40 limit to be a "+" instead of "40".

\b[0-9a-f]+\b
thethiny
  • 1,125
  • 1
  • 10
  • 26