File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ layout : post
3+ title : How to generate a random token
4+ date : 2026-02-12 18:48:33
5+ excerpt : How to generate a random token with bash.
6+ categories : bash nodejs python sha openssl uuidgen token hash uuid
7+ ---
8+
9+ This post goes over how to generate a random token with bash:
10+
11+ - [ Node.js] ( #nodejs )
12+ - [ Python] ( #python )
13+ - [ SHA] ( #sha )
14+ - [ openssl] ( #openssl )
15+ - [ uuidgen] ( #uuidgen )
16+
17+ ## Node.js
18+
19+ Generate UUID:
20+
21+ ``` sh
22+ node -p ' require("crypto").randomUUID()'
23+ ```
24+
25+ Output:
26+
27+ ```
28+ 9396db12-8449-41d6-871d-3237f9635fe3
29+ ```
30+
31+ ## Python
32+
33+ Generate URL-safe Base64 (no ` + ` and ` / ` ):
34+
35+ ``` sh
36+ python3 -c " import secrets; print(secrets.token_urlsafe(32))"
37+ ```
38+
39+ Output:
40+
41+ ```
42+ 7qwVk5Khe484EboUj_qI8pGcD-M-63Er1jK3qZS0R68
43+ ```
44+
45+ ## SHA
46+
47+ Generate hash::
48+
49+ ``` sh
50+ date | md5
51+ ```
52+
53+ Output:
54+
55+ ```
56+ 332690933cc2bebed106ca4e1c13e278
57+ ```
58+
59+ For a longer hash, replace ` md5 ` with an algorithm below:
60+
61+ - ` sha1 `
62+ - ` sha224 `
63+ - ` sha256 `
64+ - ` sha384 `
65+ - ` sha512 `
66+
67+ ## openssl
68+
69+ Generate 64 hex characters:
70+
71+ ``` sh
72+ openssl rand -hex 32
73+ ```
74+
75+ Output:
76+
77+ ```
78+ f48c74486edad3a4c1d1018dcd2f60443e38d43348e14a5be60e46f16cee0fc4
79+ ```
80+
81+ Generate Base64:
82+
83+ ``` sh
84+ openssl rand -base64 32
85+ ```
86+
87+ Output:
88+
89+ ```
90+ RzFILO5Z8S8VMJDoP7RniyjD8WR6N9ES+y665N8/nC8=
91+ ```
92+
93+ ## uuidgen
94+
95+ Generate a random-based UUID (UUIDv4):
96+
97+ ``` sh
98+ uuidgen
99+ ```
100+
101+ Output:
102+
103+ ```
104+ A004F406-00AF-4C39-9830-32668C49744B
105+ ```
You can’t perform that action at this time.
0 commit comments