-
Notifications
You must be signed in to change notification settings - Fork 605
How to random a string or a number
Vex Woo edited this page Jul 13, 2016
·
3 revisions
This document talks about how to generate a random string / number in the cleanest way possible.
To generate a random string, you need to import the library first. The following is an example of using the library in python interactive shell.
>>> from pocsuite.lib.utils import randoms
It can also be used in poc(s) files, and has core functions as follow:
-
randoms.allchars -
randoms.lowerAlpha -
randoms.upperAlpha -
randoms.numerals -
randoms.rand_base -
randoms.rand_char -
randoms.rand_item_from_iters -
randoms.rand_text -
randoms.rand_text_alpha -
randoms.rand_text_alpha_lower -
randoms.rand_text_alpha_upper -
randoms.rand_text_alphanumeric -
randoms.rand_text_numeric
generate a random string with chars collection
>>> allchars = ['a', 'b', 'c', 'x', 'y', 'z']
>>> randoms.rand_base(4, 'abcdef', allchars)
'zxxx'
generate a random char with chars collection
>>> chars = [randoms.rand_char() for i in range(5)]
>>> chars
['j', '\x9a', '.', '\xa7', '\x82']
generate a random string (cab be with unprintable chars)
>>> randoms.rand_text(4)
'\xbb\xfeF!'
generate a random string with alpha chars
>>> randoms.rand_text_alpha(4)
'vPtf'
generate a random lower string with alpha chars
>>> randoms.rand_text_alpha_lower(4)
'fhwr'
generate a random upper string with alpha chars
>>> randoms.rand_text_alpha_upper(4)
'GAZT'
generate a random string with alpha and numerals chars
>>> randoms.rand_text_alphanumeric(4)
'4xZT'
generate a random string with numerals chars
>>> randoms.rand_text_numeric(4)
'5386'
choose a random item from items
>>> abc = ['aa', 'bb', 'cc', 'dd']
>>> randoms.rand_item_from_iters(abc)
'dd'
>>> randoms.rand_item_from_iters(abc)
'bb'