Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. // generateToken
  2. // This function generates a random string at a length specified by passing in the n parameter
  3. // Customise chars to define the available characters to add to the string
  4. // Usage: generateToken(32);
  5.  
  6. function generateToken(n: 32) {
  7. var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  8. var token = '';
  9. if (!isNaN(n)) {
  10. for(var i = 0; i < n; i++) {
  11. token += chars[Math.floor(Math.random() * chars.length)];
  12. }
  13. return token;
  14. } else {
  15. console.log("Failed to generate token because the length specified must be a valid number.");
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement