bytes Mode
In bytes mode, bytes values are generated. The format of the command is as follows:
randog bytes [--length LENGTH] [--fmt FORMAT] [common-options]
Warning
This bytes generation is not guaranteed to be cryptographically secure. In cases where there are security requirements, such as the generation of tokens for authentication, consider using secrets module or similar.
Arguments and Options
--length LENGTH
(optional):the length of generated bytes. You can specify an integer such as
--length 5
or a range such as--length 3:8
.
--fmt FORMAT
(optional):if specified, it outputs generated object with the specified format; The format is specified in the format codes.
common-options
Examples
The simplest example is the following:
randog bytes --output ./result.dat
You can specify the length as follows:
# generate bytes of length 16
randog bytes --length 16 --output ./result.dat
# generate bytes of length 8 - 16
randog bytes --length 8:16 --output ./result.dat
Note
If you do not specify a format, the output will be in binary data. If you try to output to the console screen, it will not be output correctly in Windows Powershell, etc., so use the --output
option as in the example above to output to a file.
Warning
This bytes generation is not guaranteed to be cryptographically secure. In cases where there are security requirements, such as the generation of tokens for authentication, consider using secrets module or similar.
Format
By default, the output is binary data, but you can change the output format to the format specified in the format codes as follows:
# generates a value casted by str()
randog bytes --fmt s
# generates a value expressed in hexadecimal with padding zeros on the left.
randog bytes --fmt '0>20x'
You can also use the option --base64
to encode the output in base64. If you use --fmt
at the same time, the base64-encoded result is treated as a string and output in the format specified by --fmt
.
# output base64-encoded value such as 'QIj/'
randog bytes --base64
# output base64-encoded value such as ' QIj/'
randog bytes --base64 --fmt '>6s'
# output base64-encoded value such as '"QIj/"'
randog bytes --base64 --json
Repeatedly Generate
By using the -r option, it is possible to generate multiple values.
However, if you do not specify an output format, the output will be in binary format, and multiple values will be output continuously without a delimiter. To avoid this, use the –output option to output to a different file for each generation, as in the following example:
# Repeat 10 times; results are output as binary continuously without a delimiter
randog bytes -r 10
# Repeat 10 times; results are separated into different files
randog bytes -r 10 --output 'result_{0}'
Format codes
bytes objects have no format codes defined, but in this mode, you can use proprietary format codes like Standard Format Specifier.
# generates a value casted by str()
randog bytes --fmt s
# generates a value expressed in hexadecimal with padding zeros on the left.
randog bytes --fmt '0>20x'
# generates a value expressed in binary notation with 4 digits each divided by '_'.
randog bytes --fmt _b
# generates a value expressed in hexadecimal with prefix '0x'.
randog bytes --fmt '#x'
The available presentation types are:
s
: String format. The output is result of functionstr()
, such asb'ab\xff'
.c
: String format. The output is inside of result of functionstr()
, such asab\xff
.b
: Binary format. Outputs the number in base 2.x
: Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9.X
: Hex format. Outputs the number in base 16, using upper-case letters for the digits above 9. In case ‘#’ is specified, the prefix ‘0x’ will be upper-cased to ‘0X’ as well.