Skip to content

Commit e07bf69

Browse files
committed
Initial commit
0 parents  commit e07bf69

11 files changed

Lines changed: 417 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
paths:
9+
- "**.php"
10+
- "phpunit.xml"
11+
pull_request:
12+
paths:
13+
- "**.php"
14+
- "phpunit.xml"
15+
16+
jobs:
17+
test:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout Code
22+
uses: actions/checkout@v2
23+
24+
- name: Install composer and dependencies
25+
uses: php-actions/composer@v6
26+
with:
27+
php_version: "8.1"
28+
29+
- name: PHPUnit Tests
30+
uses: php-actions/phpunit@v4
31+
env:
32+
XDEBUG_MODE: coverage
33+
with:
34+
php_extensions: "xdebug"
35+
coverage_clover: "./clover.xml"
36+
testsuite: "All"
37+
38+
- name: Upload coverage reports to Codecov
39+
uses: codecov/codecov-action@v5
40+
with:
41+
token: ${{ secrets.CODECOV_TOKEN }}
42+
slug: shimoning/encryption-php
43+
files: ./clover.xml
44+
verbose: true

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
composer.lock
2+
composer.phar
3+
/vendor/
4+
5+
/.vscode/
6+
/.idea/
7+
8+
.phpunit.result.cache
9+
.phpstan.cache
10+
.php-cs-fixer.cache
11+
.coverage
12+
clover.xml
13+
14+
.env

.php-cs-fixer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in([
5+
__DIR__ . '/src',
6+
]);
7+
8+
return (new PhpCsFixer\Config())
9+
->setRiskyAllowed(true)
10+
->setRules([
11+
'@PSR12' => true,
12+
'concat_space' => [
13+
'spacing' => 'one',
14+
],
15+
'binary_operator_spaces' => [
16+
'default' => 'single_space',
17+
'operators' => [
18+
'=' => 'align_single_space_minimal',
19+
'=>' => 'align_single_space_minimal',
20+
],
21+
],
22+
])
23+
->setFinder($finder);

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Shimoning Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# PHP 用の簡易可逆暗号化ライブラリ
2+
3+
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
4+
![PHP8.1](https://img.shields.io/badge/-PHP8.1-777BB4.svg?style=flat&logo=php&labelColor=777BB4&logoColor=FFF)
5+
[![codecov](https://codecov.io/gh/shimoning/encryption-php/graph/badge.svg)](https://codecov.io/gh/shimoning/encryption-php)
6+
7+
## 動作環境
8+
9+
- PHP >= 8.1
10+
11+
## Install
12+
13+
### composer で追加する
14+
15+
利用するプロジェクトの `composer.json` に以下を追加する。
16+
17+
```composer.json
18+
"repositories": {
19+
"shimoning/encryption": {
20+
"type": "vcs",
21+
"url": "https://github.com/shimoning/encryption-php.git"
22+
}
23+
},
24+
```
25+
26+
その後以下でインストールする。
27+
28+
```bash
29+
composer require shimoning/encryption
30+
```
31+
32+
## 実装
33+
34+
### 暗号化
35+
36+
1. 文字列を openssl で暗号化
37+
2. それを base64 でエンコード
38+
39+
### 復号化
40+
41+
1. 文字列を base64 でデコード
42+
2. それを openssl で復号化

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "shimoning/encryption",
3+
"description": "Library about encrypt",
4+
"keywords": [
5+
"encryption",
6+
"encrypt",
7+
"decrypt"
8+
],
9+
"type": "library",
10+
"version": "0.1.0",
11+
"license": "MIT",
12+
"require": {
13+
"php": ">=8.1"
14+
},
15+
"require-dev": {
16+
"friendsofphp/php-cs-fixer": "^2 | ^3",
17+
"phpunit/phpunit": "^10.5",
18+
"phpstan/phpstan": "^2.1",
19+
"phpmd/phpmd": "^2.15"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Shimoning\\Encryption\\": "src"
24+
}
25+
},
26+
"scripts": {
27+
"test": "phpunit",
28+
"coverage": "XDEBUG_MODE=coverage phpunit --coverage-html='.coverage' --coverage-clover='clover.xml'",
29+
"cs:check": "php-cs-fixer fix --dry-run --diff --verbose",
30+
"cs:fix": "php-cs-fixer fix --verbose",
31+
"md": "phpmd src text phpmd.xml",
32+
"stan": "phpstan analyse"
33+
}
34+
}

phpmd.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="freee-webhook PHPMD rule set"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
8+
<description>freee-webhook phpmd custom rules</description>
9+
10+
<rule ref="rulesets/cleancode.xml">
11+
<exclude name="StaticAccess" />
12+
<exclude name="BooleanArgumentFlag" />
13+
</rule>
14+
<rule ref="rulesets/codesize.xml" />
15+
<rule ref="rulesets/controversial.xml" />
16+
<rule ref="rulesets/design.xml" />
17+
<rule ref="rulesets/unusedcode.xml" />
18+
<rule ref="rulesets/naming.xml">
19+
<exclude name="ShortVariable" />
20+
</rule>
21+
<rule ref="rulesets/naming.xml/ShortVariable">
22+
<properties>
23+
<property name="minimum" value="2" />
24+
</properties>
25+
</rule>
26+
</ruleset>

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
tmpDir: %currentWorkingDirectory%/.phpstan.cache

phpunit.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
stopOnFailure="false"
6+
displayDetailsOnPhpunitDeprecations="true">
7+
8+
<testsuites>
9+
<testsuite name="All">
10+
<directory suffix="Test.php">tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<source>
15+
<include>
16+
<directory suffix=".php">src</directory>
17+
</include>
18+
</source>
19+
</phpunit>

src/Encryption.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Shimoning\Encryption;
4+
5+
class Encryption
6+
{
7+
/**
8+
* Default encryption method.
9+
*/
10+
public const DEFAULT_METHOD = 'AES-256-CBC';
11+
12+
/**
13+
* Default key for encryption.
14+
*
15+
* \base64_encode(\openssl_random_pseudo_bytes(\openssl_cipher_iv_length('AES-256-CBC')))
16+
*/
17+
public const DEFAULT_KEY = '06ShagteTfh1cKgu0GI0iA==';
18+
19+
/**
20+
* Default IV for encryption.
21+
*
22+
* \base64_encode(\openssl_random_pseudo_bytes(\openssl_cipher_iv_length('AES-256-CBC')))
23+
*/
24+
public const DEFAULT_IV = 'lSwJdjw1PUnjyw5OwhAUIA==';
25+
26+
/**
27+
* 暗号化を行う
28+
*
29+
* @param string $string
30+
* @param string|null $method
31+
* @param string|null $base64key
32+
* @param string|null $base64iv
33+
* @return string
34+
*/
35+
public static function encrypt(
36+
string $string,
37+
?string $method = null,
38+
?string $base64key = null,
39+
?string $base64iv = null,
40+
): string {
41+
return \base64_encode(
42+
\openssl_encrypt(
43+
$string,
44+
$method ?? static::DEFAULT_METHOD,
45+
\base64_decode($base64key ?? static::DEFAULT_KEY),
46+
\OPENSSL_RAW_DATA,
47+
\base64_decode($base64iv ?? static::DEFAULT_IV),
48+
),
49+
);
50+
}
51+
52+
/**
53+
* 復号化を行う
54+
*
55+
* @param string $string
56+
* @param string|null $method
57+
* @param string|null $base64key
58+
* @param string|null $base64iv
59+
* @return string
60+
*/
61+
public static function decrypt(
62+
string $string,
63+
?string $method = null,
64+
?string $base64key = null,
65+
?string $base64iv = null,
66+
): string {
67+
return \openssl_decrypt(
68+
\base64_decode($string),
69+
$method ?? static::DEFAULT_METHOD,
70+
\base64_decode($base64key ?? static::DEFAULT_KEY),
71+
\OPENSSL_RAW_DATA,
72+
\base64_decode($base64iv ?? static::DEFAULT_IV),
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)