Coder Social home page Coder Social logo

manwar / mojolicious-plugin-util-randomstring Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akron/mojolicious-plugin-util-randomstring

0.0 3.0 0.0 22 KB

Generate Secure Random Strings in Mojolicious

License: Artistic License 2.0

Perl 100.00%

mojolicious-plugin-util-randomstring's Introduction

NAME

Mojolicious::Plugin::Util::RandomString - Generate Secure Random Strings for Mojolicious

SYNOPSIS

# Mojolicious::Lite
plugin 'Util::RandomString' => {
  entropy => 256,
  printable => {
    alphabet => '2345679bdfhmnprtFGHJLMNPRT',
    length   => 20
  }
};

# Generate string with default configuration
<%= random_string %>

# Generate string with 'printable' configuration
<%= random_string 'printable' %>

# Generate string with 'printable' configuration
# and overwrite length
<%= random_string 'printable', length => 16 %>

# Generate string with default configuration
# and overwrite character set in a Controller
$c->random_string(alphabet => ['a' .. 'z']);

DESCRIPTION

Mojolicious::Plugin::Util::RandomString is a plugin to generate random strings for session tokens, encryption salt, temporary password generation etc. Internally it uses Session::Token (see this comparison for reasons for this decision).

This plugin will automatically reseed the random number generator in a forking environment like Hypnotoad (although it is untested in other forking environments that don't use Mojo::IOLoop).

METHODS

Mojolicious::Plugin::Util::RandomString inherits all methods from Mojolicious::Plugin and implements the following new one.

register

# Mojolicious
$app->plugin('Util::RandomString');

# Mojolicious::Lite
plugin 'Util::RandomString' => {
  entropy => 256,
  printable => {
    alphabet => '2345679bdfhmnprtFGHJLMNPRT',
    length   => 20
  }
};

# Or in your config file
{
  'Util-RandomString' => {
    entropy => 256,
    printable => {
      alphabet => '2345679bdfhmnprtFGHJLMNPRT',
      length   => 20
    }
  }
}

Called when registering the plugin. Expects a hash reference containing parameters as defined in Session::Token for the default generator. To specify named generators, use a name key (other than alphabet, length, and entropy) and specify the parameters as a hash reference. The name key 'default' can overwrite the default configuration.

All parameters can be set either on registration or as part of the configuration file with the key Util-RandomString.

The plugin can be registered multiple times with different, overwriting configurations.

The default alphabet is base62. This is good for a lot of use cases. If you want to generate human readable tokens, you can define another scheme (e.g. the above shown 'printable' base26 scheme with a character set with visually distinctive characters, that also makes it unlikely to generate insulting words due to missing vocals).

HELPERS

random_string

# In Controller
print $c->random_string;
print $c->random_string('printable');
print $c->random_string(length => 45)
print $c->random_string('printable', length => 45)

# In Template
%= random_string;
%= random_string('printable');
%= random_string(length => 45)
%= random_string('printable', length => 45)

Generate a random string. In case of no parameters, the default configuration is used. In case of one parameter, this is treated as the key of a chosen configuration. The following parameters can be used to modify a given configuration for one request (but please note: each modified request creates a new and seeded Session::Token generator, which is bad for performance).

DEPENDENCIES

Mojolicious (best with SSL support), Session::Token.

AVAILABILITY

https://github.com/Akron/Mojolicious-Plugin-Util-RandomString

COPYRIGHT AND LICENSE

Copyright (C) 2013-2018, Nils Diewald.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

mojolicious-plugin-util-randomstring's People

Contributors

akron avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.