搜索插件、代码仓与 APP 分类

插件详情v0.1.2

shortid

ShortId 创建短的、非顺序的、URL 友好的唯一 ID。非常适合 URL 缩略器、MongoDB 和 Redis ID 等场景。(来自 JS 的移植)

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
24万
喜欢数
21
Pub 分数
150

安装插件

flutter pub add shortid

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

ShortId 生成短小、非顺序且适合 URL 的唯一标识符,非常适合用于 URL 缩略器、MongoDB 和 Redis 等数据库,以及其他需要紧凑安全 ID 的系统。该包是流行的 JavaScript 库的 Dart 版本,确保唯一性并避免可预测的模式。该包轻量且兼容所有主要平台,适用于需要可靠 ID 生成而无需依赖顺序或长标识符的跨平台应用。

适合场景

  • 网址缩短服务
  • 数据库主键
  • 会话令牌生成
  • 缓存密钥创建
  • 安全标识符生成

主要特点

  • 生成短随机ID
  • URL安全字符
  • 非顺序且不可预测
  • 跨平台支持
  • 轻便且快速
  • JavaScript 库的 Dart 版本

适合对象

  • 需要紧凑 ID 的 Web 和移动应用
  • 后端服务与数据库集成
  • 需要安全且无法猜测的标识符的应用程序
  • 使用 MongoDB 或 Redis 的项目
  • 熟悉 JavaScript ShortId 的开发者

使用建议

  • 不适合高安全性使用场景的加密安全
  • ID 在没有额外检查的情况下不能保证全局唯一
  • 取决于随机数生成的质量
  • 限制为16个字符的默认长度
  • 无内置碰撞检测

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.

  • By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _-
  • Supports custom seeds, custom alphabet.
  • Can generate any number of ids without duplicates, even millions per day.
  • Good replacement for Mongo ID/Mongoose ID.
  • Includes tests.

ShortId does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.

Usage
import 'package:shortid/shortid.dart';

print(shortid.generate());
// PPBqWA9

shortid.generate()

Returns string non-sequential unique id.

Example

users.insert({
  _id: shortid.generate(),
  name: '...',
  email: '...'
});

shortid.characters(string)

Default: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'

Returns new alphabet as a string

Recommendation: If you don't like _ or -, you can to set new characters to use.

Optional

Change the characters used.

You must provide a string of all 64 unique characters. Order is not important.

The default characters provided were selected because they are url safe.

Example

// use $ and @ instead of - and _
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
// any 64 unicode characters work, but I wouldn't recommend this.
shortid.characters('ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫');

shortid.seed(integer)

Default: 1

Recommendation: You typically won't want to change this.

Optional

Choose a unique value that will seed the random number generator so users won't be able to figure out the pattern of the unique ids. Call it just once in your application before using shortId and always use the same value in your application.

Most developers won't need to use this, it's mainly for testing ShortId.

If you are worried about users somehow decrypting the id then use it as a secret value for increased encryption.

Example

shortid.seed(1000);

所属分类

源码仓库

使用此插件的项目