定制 originphp/security 二次开发

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

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

originphp/security

Composer 安装命令:

composer require originphp/security

包简介

OriginPHP Security

README 文档

README

license build coverage

The Security library provides various function related to security such as hashing, encryption, decryption and secure random string generation.

Installation

To install this package

$ composer require originphp/security

Hashing

Hashing Strings (Not Passwords)

The default hashing algorithm used is sha256.

To hash a string (not password)

use Origin\Security\Security;
$hashed = Security::hash('foo');

To hash a string with a pepper (also incorrectly known as salt).

$hashed = Security::hash('foo',['pepper'=>'A random string']);

To change the hashing type

$hashed = Security::hash('foo',['type'=>'sha1']);

For a full list of supported algorithms

$list = hash_algos()

Hashing Passwords

The Security utility hashes passwords using best practices, currently this is bcrypt which is considered very secure.

$hashed = Security::hashPassword('secret');

To verify the password is correct

$hashed = Security::hashPassword('secret');
$bool = Security::verifyPassword($input,$hashed); // input is user inputted password

Encryption

Generating a Secure Key

To encrypt and decrypt a string you will need a key, you can generate a random secure key.

use Origin\Security\Security;
$key = Security::generateKey(); // ESaCestIJvuAo3NUAtHAZG9DqmFJZtyx

The key length must be 32 bytes (256 bits) to use with the encryption decryption functions.

Encrypting Text

To encrypt a string

use Origin\Security\Security;
$key = '33d80476167cc95c363bf7df3c95e1d1';
$encrypted = Security::encrypt('foo',$key);

Decrypting Text

To decrypt an encrypted string

use Origin\Security\Security;
$key = '33d80476167cc95c363bf7df3c95e1d1';
$encrypted = 'ohRRdAydx+4wfOd7Vm+LHmmV9zBH+3r0WLQylyPMPu2RvCjX9FVgoeUBZuLYBTLM4x9NeZX7U0bUvE1bucATSQ==';
$plain = Security::decrypt($encrypted,$key);

Random Strings

To generate a cryptographically secure hexadecimal random string, the default length is 16. (This is an alias for hex)

$randomString = Security::random(); // 5f31ecf661dabb04

Different Encodings

You can generate secure random strings with different encoding.

$hex = Security::hex(); // gpgf67ezotl06wqs
$base36 = Security::base36(); // 13owqvwcgb426rvq
$base58 = Security::base58(); // SyqBFAtGfNxZkZMQ
$base62 = Security::base62(); // oc1eIfAHKWWt5zrO
$base64 = Security::base64(); // v3xsI6O+g6LsuY4+
// url safe
$base64 = Security::base64(16,true); // YPT9rp-i6jqXWCvA

UID

If you need to generate a unique id, and don't need to use a UUID, then the UID method provides a more memory and disk space efficient way when working with unique ids.

If you are generating a API token or another form of string that a user might need to type in, then use Security::random or Security::uuid instead since these use lower case characters.

To generate a cryptographically secure unique id (UID) using base62 with a default length of 16.

$uid = Security::uid(); // O64cjBxfz2JPhyCQ

UUID

The Security class can generate both version 4 and version 1 UUIDs.

To generate a random UUID (version 4)

$uid = Security::uuid(); // 38c67382-d3ab-4430-a27e-0c719813c09f

For a version 1 UUID, set MAC address to true, this will try to find the MAC address on Linux systems or generate a random one.

$uid = Security::uuid(['macAddress'=>true]); // ac337932-e4e5-11e9-928f-8bda39fe8887

You can also set the MAC address manually.

$uid = Security::uuid(['macAddress'=>'00:0a:95:9d:68:16']); // 769c6fa4-e4e5-11e9-b8d5-000a959d6816

originphp/security 适用场景与选型建议

originphp/security 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.39k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「hash」 「encryption」 「base64」 「hashing」 「uuid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 originphp/security 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 originphp/security 我们能提供哪些服务?
定制开发 / 二次开发

基于 originphp/security 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-16