soatok / minisign
基于libsodium的minisign的PHP实现
v0.6.0
2024-04-21 01:58 UTC
Requires
- php: ^7.2|^8
- ext-sodium: *
- paragonie/constant_time_encoding: ^2
- paragonie/sodium_compat: ^1|^2
- ulrichsg/getopt-php: ^3
Requires (Dev)
- phpunit/phpunit: ^8|^9
- vimeo/psalm: ^3|^4
This package is auto-updated.
Last update: 2024-09-21 02:51:01 UTC
README
PHP版本的Minisign。由Libsodium驱动。
安装
composer require soatok/minisign
使用(命令行)
创建密钥对
vendor/bin/minisign -G
签名文件
vendor/bin/minisign -Sm myfile.txt
或要包括签名中的注释,该注释将在验证文件时被验证并显示
vendor/bin/minisign -Sm myfile.txt -t 'This comment will be signed as well'
签名将被放入myfile.txt.minisig中。
也可以一次性对多个文件进行签名
vendor/bin/minisign -Sm file1.txt file2.txt *.jpg
验证文件
vendor/bin/minisign -Vm myfile.txt -P RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3
或
vendor/bin/minisign -Vm myfile.txt -p signature.pub
这需要签名myfile.txt.minisig
存在于同一目录中。
公钥可以存储在文件中(默认为./minisign.pub
)或直接在命令行中指定。
使用(PHP代码)
创建密钥对
<?php use Soatok\Minisign\Core\SecretKey; $secretKey = SecretKey::generate(); $password = 'correct horse battery staple'; $saveToFile = $secretKey->serialize($password); \file_put_contents('/path/to/secret.key', $saveToFile);
签名文件
<?php use Soatok\Minisign\Core\SecretKey; use Soatok\Minisign\Core\File\MessageFile; $trustedComment = 'Trusted comment goes here'; $untrustedComment = 'Untrusted comment; can be changed'; $password = 'correct horse battery staple'; $preHash = false; // Set to TRUE to prehash the file $secretKey = SecretKey::fromFile('/path/to/secret.key', $password); $fileToSign = MessageFile::fromFile('/path/to/file'); $signature = $fileToSign->sign( $secretKey, $preHash, $trustedComment, $untrustedComment ); \file_put_contents( '/path/to/file.minisig', $signature->toSigFile()->getContents() );
验证文件
<?php use Soatok\Minisign\Core\PublicKey; use Soatok\Minisign\Core\File\{ MessageFile, SigFile }; $pk = PublicKey::fromFile('/path/to/minisign.pub'); $fileToCheck = MessageFile::fromFile('/path/to/file'); $signature = SigFile::fromFile('/path/to/file.minisig')->deserialize(); if (!$fileToCheck->verify($pk, $signature)) { echo 'Invalid signature!', PHP_EOL; exit(1); } $trusted = $signature->getTrustedComment();