saacsos/randomgenerator

根据等级和长度生成随机字符串

v1.1.0 2016-05-09 11:17 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:46:50 UTC


README

Laravel 5 自定义字符串随机生成器

安装

$ composer require saacsos/randomgenerator

基本用法

<?php
use Saacsos\Randomgenerator\Util\RandomGenerator;

// Create an object
$randomGenerator = new RandomGenerator();

// Get random password
$randomGenerator->password();
$password = $randomGenerator->get();
// or
$password = $randomGenerator->password()->get();

// Get random access token (48 chars)
$accessToken = $randomGenerator->accessToken()->get();

等级 & 长度

强度等级

  • 1 = 数字 ([0-9])
  • 2 = 十六进制 ([0-9a-f])
  • 4 = 小写 (a-z except i,l,o)
  • 8 = 大写 (A-Z except I,L,O)
  • 16 = 特殊字符 !@#$%^&*()_=[]{}?, 结合等级以生成复杂密码
  • 5 = 1 + 4 = 数字 + 小写
  • 13 = 1 + 4 + 8 = 数字 + 小写 + 大写
  • 29 = 1 + 4 + 8 + 16 = 数字 + 小写 + 大写 + 特殊字符
<?php

// get password 8 characters in level 5 
$password = $randomGenerator->level(5)->length(8)->password()->get();

默认等级 = 13,默认长度 = 8

isMatch($string, $strict=false)

验证 $string 是否符合等级要求。严格模式将检查 $string 是否至少包含等级中的一个字符

<?php

$match = $randomGenerator->min(8)->max(8)->level(13)->isMatch('password'); // true
$match = $randomGenerator->min(8)->max(8)->level(13)->isMatch('password', true); // false because no uppercase

Laravel 5 服务提供者和 Facades

将您的新提供者添加到 config/app.phpproviders 数组中

    'providers' => [
        // ...
        Saacsos\Randomgenerator\ServiceProvider\RandomGeneratorServiceProvider::class,
        // ...
    ],

将类别名添加到 config/app.phpaliases 数组中

    'aliases' => [
        // ...
        'RandomGenerator' => Saacsos\Randomgenerator\Facades\RandomGenerator::class,
        // ...
    ],

然后您可以使用

    $password = \RandomGenerator::password()->get();