keygen_Laravel随机密钥与Keygen

本文介绍了Keygen PHP包在Laravel应用中的使用,它用于生成简单随机字符序列。Keygen提供了四种生成器:numeric、alphanumeric、token和bytes,支持无缝键后缀、键转换和键突变。教程涵盖了从安装Keygen到创建用户模型、定义API路由、生成用户唯一ID和代码,以及测试API的过程。通过Keygen,开发者可以更方便地在应用中生成随机密钥。
摘要由CSDN通过智能技术生成

keygen

When developing applications, it is usually common to see randomness come into play - and as a result, many programming languages have built-in random generation mechanisms. Some common applications include:

开发应用程序时,通常会看到随机性发挥作用-因此,许多编程语言都内置了随机生成机制。 一些常见的应用程序包括:

  • Generating a random numeric code for email confirmation or phone number verification service.

    生成用于电子邮件确认或电话号码验证服务的随机数字代码。
  • Password generation service that generates random alphanumeric password strings.

    密码生成服务,生成随机的字母数字密码字符串。
  • Generating random base64-encoded tokens or strings as API keys.

    生成随机的base64编码的标记或字符串作为API密钥。
  • Generating random strings as password salts to hash user passwords.

    生成随机字符串作为密码盐来哈希用户密码。

When your application is required to generate very simple random character sequences like those enumerated above, then the Keygen package is a good option to go for.

当需要您的应用程序生成非常简单的随机字符序列(如上述枚举序列)时, Keygen包是一个不错的选择。

介绍Keygen软件包 ( Introducing the Keygen Package )

Keygen is a PHP package for generating simple random character sequences of any desired length and it ships with four generators, namely: numeric, alphanumeric, token and bytes. It has a very simple interface and supports method chaining - making it possible to generate simple random keys with just one line of code. The Keygen package can save you some time trying to implement a custom random generation mechanism for your application. Here are some added benefits of the Keygen package:

Keygen是一个PHP软件包,用于生成任意所需长度的简单随机字符序列,并附带四个生成器,即: numericalphanumerictokenbytes 。 它具有非常简单的界面,并支持方法链接 -使得仅用一行代码即可生成简单的随机密钥。 Keygen程序包可以为您节省一些时间,以尝试为您的应用程序实现自定义随机生成机制。 以下是Keygen软件包的一些其他优点:

  • Seamless key affixes: It's very easy to add a prefix or suffix to the random generated string.

    无缝键 后缀:在随机生成的字符串中添加前缀后缀非常容易。
  • Key Transformations: You can process the random generated string through a queue of callables before it is finally outputted.

    关键转换:您可以在最终输出之前,通过可调用队列处理随机生成的字符串。
  • Key Mutations: You can control manipulations and mutations of multiple Keygen instances.

    密钥突变:您可以控制多个Keygen实例的操纵和突变。

This tutorial provides a quick guide on how you can get started with the Keygen package and using it in your Laravel applications. For a complete documentation and usage guide of the Keygen package, see the README document at Github.

本教程提供了有关如何快速入门Keygen软件包以及如何在Laravel应用程序中使用它的快速指南。 有关Keygen软件包的完整文档和使用指南,请参阅Github上的README文档。

入门 ( Getting Started )

In this tutorial, we would be creating a simple REST API service. The API simply provides endpoints for creating user record, showing user record and generating a random password.

在本教程中,我们将创建一个简单的REST API服务。 该API仅提供了用于创建用户记录,显示用户记录和生成随机密码的端点。

This tutorial assumes you already have a Laravel application running and the Composer tool is installed in your system and added to your system PATH. In this tutorial, I am using Laravel 5.3, which is the latest stable version at this time of writing. You can refer to the Laravel Installation guide if you don't have Laravel installed.

本教程假定您已经在运行Laravel应用程序,并且Composer工具已安装在系统中并已添加到系统PATH中。 在本教程中,我正在使用Laravel 5.3,它是撰写本文时的最新稳定版本。 如果未安装Laravel,则可以参考《 Laravel安装指南》。

Next, we would install the Keygen package as a dependency for our project using composer. The Keygen package is available on the Packagist repository as gladcodes/keygen.

接下来,我们将使用composer将Keygen软件包安装为项目的依赖项。 Keygen软件包可在Packagist存储库中以gladcodes / keygen的形式获得

composer require gladcodes/keygen

If it installed correctly, you should see a screen like the following screenshot.

如果安装正确,您应该会看到类似以下屏幕截图的屏幕。

Keygen Installation Screenshot

为Keygen包创建别名 ( Creating an alias for the Keygen package )

The functionality of the Keygen package is encapsulated in the Keygen\Keygen class. For convenience, we would register an alias for this class, so that we can easily use it anywhere in our application. To create the alias, we would edit the config/app.php file and add a record for our alias after the last record in the aliases array as shown in the following snippet.

Keygen程序包的功能封装在Keygen\Keygen类中。 为了方便起见,我们将为此类注册一个别名,以便我们可以在应用程序中的任何位置轻松使用它。 要创建别名,我们将编辑config/app.php文件,并在aliases数组中的最后一条记录之后为别名添加一条记录,如以下代码片段所示。

// config/app.php

'aliases' => [
    // ... other alias records
    'Keygen' => Keygen\Keygen::class,
],

Now we can use the Keygen package anywhere in our application. Add the use Keygen directive in your code to use the Keygen package as shown in the following usage example code.

现在,我们可以在应用程序中的任何地方使用Keygen软件包。 在代码中添加use Keygen指令以使用Keygen包,如以下用法示例代码所示。

// usage example<?php

use Keygen;

$id = Keygen::numeric(10)->generate();
echo $id; //2542831057

创建用户模型 ( Creating the User Model )

Next, we would create a database table called users to store our users records. The schema for the table is as follows:

接下来,我们将创建一个称为users的数据库表来存储我们的用户记录。 该表的架构如下:

  • id INT(11) NOT NULL PRIMARY

    id INT(11) NOT NULL PRIMARY
  • code CHAR(24) NOT NULL UNIQUE

    code CHAR(24) NOT NULL UNIQUE
  • firstname VARCHAR(32) NOT NULL

    firstname VARCHAR(32) NOT NULL
  • lastname VARCHAR(32) NOT NULL

    lastname VARCHAR(32) NOT NULL
  • email VARCHAR(80) NOT NULL UNIQUE

    email VARCHAR(80) NOT NULL UNIQUE
  • password_salt CHAR(64) NOT NULL

    password_salt CHAR(64) NOT NULL
  • password_hash CHAR(60) NOT NULL

    password_hash CHAR(60) NOT NULL

What about autoincrement?
For this tutorial, the id of our users table would be a unique random generated integer, just to demonstrate with the Keygen package. This choice is based on preference, and does not in anyway discourage the use of auto-incremented IDs.

autoincrement呢?
在本教程中,我们的users表的id将是一个唯一的随机生成的整数,仅用于Keygen包的演示。 此选择基于首选项,无论如何不妨碍使用自动递增的ID。

If created correctly, it should be as shown in the following screens

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值