maxoplata/string-thing

一个轻量级的库,用于使用各种模式进行字符串的编码和解码。

v1.0.0 2023-04-21 04:58 UTC

This package is not auto-updated.

Last update: 2024-09-21 10:18:28 UTC


README

StringThing是一个轻量级的库,用于使用各种模式进行字符串的编码和解码。

安装

composer require string-thing

使用

StringThing提供了一个API用于字符串的编码和解码。要使用它,导入StringThing类并创建一个新实例,其中包含一个数组,包含您希望使用的模式顺序

use Maxoplata\StringThing;

$myString = 'This is my string';

// Create a new instance of StringThing with default pattern (['split-halves', 'reverse', 'shift', 'swap-case', 'rotate'])
$myStringThing = new StringThing();

// Encode the string
$encoded = $myStringThing->encode($myString);

// Output the encoded string
print encoded; // "ZN!TJ!TJIuHOJSUT!"

// Decode the string
$decoded = myStringThing->decode($encoded);

// Output the decoded string
print decoded; // "This is my string"

模式

StringThing当前支持以下操作

  • split-halves:将字符串分成两半并交换它们。
    • Abcd12 => d12Abc
  • reverse:反转字符串中字符的顺序。
    • Abcd12 => 21dcbA
  • shift:将字符串中的字符在ASCII表中向上移动1。
    • Abcd12 => Bcde23
  • swap-case:交换字符串中的大写和小写字符。
    • Abcd12 => aBCD12
  • rotate:将字符串向右移动1位。
    • Abcd12 => 2Abcd1

要使用特定模式,将其作为参数传递给StringThing构造函数

use Maxoplata\StringThing;

$myStringThing1 = new StringThing(['split-halves', 'shift', 'reverse', 'shift', 'swap-case', 'rotate']);

// OR

$stringThingPattern = ['split-halves', 'shift', 'reverse', 'shift', 'swap-case', 'rotate'];
$myStringThing2 = new StringThing($stringThingPattern);

示例:对密码进行编码以进行安全存储

StringThing可以在将密码散列并存储在数据库之前对其进行编码,使得即使攻击者获得了对数据库的访问权限,也很难检索原始密码。

以下是一个示例,说明如何在处理数据库中的密码时,使用StringThing在散列之前对密码进行编码

创建用户

use Maxoplata\StringThing;

$stringThingPattern = ['split-halves', 'shift', 'reverse', 'shift', 'swap-case', 'rotate'];

// The original password to be encoded and hashed
$password = 'myPassword123';

// Encode the password using StringThing
$encodedPassword = (new StringThing($stringThingPattern))->encode($password);

// Hash the encoded password with bcrypt
$hashedPassword = password_hash($encodedPassword, PASSWORD_BCRYPT);

// Add the hashed password to a user object for storage in a database
$user = [
  'username' => 'johndoe',
  'email' => 'johndoe@example.com',
  'password' => hashedPassword,
  // other user data...
];

// Add the user object to the database
myDatabase->addUser($user);

用户认证

use Maxoplata\StringThing;

$stringThingPattern = ['split-halves', 'shift', 'reverse', 'shift', 'swap-case', 'rotate'];

// Retrieve the user's hashed password and salt from the database
$user = $myDatabase->getUserByUsername('johndoe');
$hashedPassword = $user->password;

// The password entered by the user attempting to log in
$passwordAttempt = 'myPassword123';

// Encode the password attempt using StringThing
$encodedPasswordAttempt = (new StringThing($stringThingPattern))->encode($passwordAttempt);

// Hash the encoded password attempt with bcrypt
$hashedPasswordAttempt = password_hash($encodedPasswordAttempt, PASSWORD_BCRYPT);

// Compare the hashed password attempt to the stored hashed password
if ($hashedPasswordAttempt === $hashedPassword) {
  // Passwords match - login successful!
} else {
  // Passwords do not match - login failed
}