定制 better/nanoid 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

better/nanoid

最新稳定版本:0.0.1

Composer 安装命令:

composer require better/nanoid

包简介

A copy of nanoid in PHP

README 文档

README

Overview

Replacement of hidehalo/nanoid-php to work with PHP 7.1+

A tiny (179 bytes), secure URL-friendly unique string ID generator for JavaScript

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Small. Only 179 bytes (minified and gzipped). No dependencies. It uses Size Limit to control size.

Compact. It uses more symbols than UUID (A-Za-z0-9_-) and has the same number of unique options in just 21 symbols instead of 36.

Install

Via Composer

composer require better/nanoid

Usage

Normal

The main module uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters (to have the same collisions probability as UUID v4).

use Better\Nanoid\Client;
use Better\Nanoid\GeneratorInterface;

$client = new Client();

# default random generator
echo $client->produce($size = 32);

# more secure generator with more entropy
echo $client->produce($size = 32, true);

Custom Alphabet or Length

$client = new Client('0123456789abcdefg');
$client->produce();

Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.

Custom Random Bytes Generator

# PS: anonymous class is new feature when PHP_VERSION >= 7.0

$client = new Client();

echo $client->produceUsing(new class implements GeneratorInterface {
    /**
     * @inheritDoc
     */
    public function random(int $size): string
    {
        $ret = [];
        
        while ($size--) {
            $ret[] = mt_rand(0, 255);
        }

        return $ret;
    }
});

random callback must accept the array size and return an array with random numbers.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 2.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-09