Skip to content

Commit

Permalink
Merge pull request #1 from mnavarrocarter/2.x
Browse files Browse the repository at this point in the history
Version 2.0.0 implemented.
  • Loading branch information
mnavarrocarter authored Nov 9, 2020
2 parents 2a8d46e + e7a0d75 commit 757e83e
Show file tree
Hide file tree
Showing 19 changed files with 425 additions and 454 deletions.
44 changes: 0 additions & 44 deletions .github/workflows/main.yml

This file was deleted.

112 changes: 112 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: "Review PR"

on:
pull_request:

jobs:

# This job ensures the coding standard is followed
coding-standards:
name: "Coding Standards"
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
php-version:
- "7.2"
operating-system:
- "ubuntu-latest"
steps:
- name: "Checkout Code"
uses: "actions/checkout@v2"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
- name: "Use Cache"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
vendor
key: "php-${{ matrix.php-version }}-locked"
restore-keys: "php-${{ matrix.php-version }}-locked"
- name: "Install locked dependencies"
run: "composer install --no-interaction --no-progress --no-suggest"
- name: "Coding Standard"
run: "vendor/bin/php-cs-fixer fix --dry-run -vvv"

type-analysis:
name: "Type Coverage"
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
php-version:
- "7.2"
operating-system:
- "ubuntu-latest"
steps:
- name: "Checkout Code"
uses: "actions/checkout@v2"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
- name: "Use Cache"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
vendor
key: "php-${{ matrix.php-version }}-locked"
restore-keys: "php-${{ matrix.php-version }}-locked"
- name: "Install locked dependencies"
run: "composer install --no-interaction --no-progress --no-suggest"
- name: "Run Psalm"
run: "vendor/bin/psalm --output-format=github --shepherd --stats"

unit-test:
name: "Unit Testing"
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
dependencies:
- "locked"
- "highest"
php-version:
- "7.2"
- "7.3"
- "7.4"
operating-system:
- "ubuntu-latest"
steps:
- name: "Checkout Code"
uses: "actions/checkout@v2"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
- name: "Use Cache"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
vendor
key: "php-${{ matrix.php-version }}-locked"
restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
- name: "Install lowest dependencies"
if: ${{ matrix.dependencies == 'lowest' }}
run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest"
- name: "Install highest dependencies"
if: ${{ matrix.dependencies == 'highest' }}
run: "composer update --no-interaction --no-progress --no-suggest"
- name: "Install locked dependencies"
if: ${{ matrix.dependencies == 'locked' }}
run: "composer install --no-interaction --no-progress --no-suggest"
- name: "Run PHPUnit"
run: "vendor/bin/phpunit --testdox --coverage-text"
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ composer require mnavarrocarter/fernet
At the moment, there is only one version of Fernet:

```php
use MNC\Fernet\Fernet;
use MNC\Fernet\Vx80Marshaller;
use MNC\Fernet\Vx80Key;

require_once __DIR__. '/vendor/autoload.php';

// Instantiate the version x80 passing the key
$fernet = Fernet::vx80('eLh6lGOYbbHvTHhI-nd_s76mZ7NZi9L5AA_bQNI_KoE');
// Instantiate a key for version x80
$key = Vx80Key::fromString('eLh6lGOYbbHvTHhI-nd_s76mZ7NZi9L5AA_bQNI_KoE');
// Then, create the marshaller
$marshaller = new Vx80Marshaller($key);

// Encode a message and get a token
$token = $fernet->encode('hello');
$token = $marshaller->encode('hello');

// You can then decrypt that token back to the message
$message = $fernet->decode($token);
$message = $marshaller->decode($token);
```

## What is Fernet?
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
],
"require": {
"php": ">=7.2",
"ext-openssl": "*"
"ext-openssl": "*",
"lcobucci/clock": "^1.2"
},
"autoload": {
"psr-4": {
"MNC\\Fernet\\": "src"
}
},
"files": ["functions.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
67 changes: 67 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace MNC\Fernet\UrlBase64;

use InvalidArgumentException;

/**
* @param string $data
* @return string
*/
function encode(string $data): string
{
return str_replace(['+', '/'], ['-', '_'], base64_encode($data));
}

/**
* @param string $base64
* @return string
* @throws InvalidArgumentException on decoding error
*/
function decode(string $base64): string
{
$result = base64_decode(str_replace(['-', '_'], ['+', '/'], $base64), true);
if (!is_string($result)) {
throw new \InvalidArgumentException('Invalid url encoded base64 string provided');
}

return $result;
}


namespace MNC\Fernet\Str;

use Exception;
use InvalidArgumentException;

/**
* Pads a message to a multiple of 16 bytes.
*
* @param string $message
*
* @return string
*/
function pad(string $message): string
{
$pad = 16 - (strlen($message) % 16);
$message .= str_repeat(chr($pad), $pad);
return $message;
}

/**
* Removed the padding of a message.
*
* @param string $paddedMessage
*
* @return string
* @throws InvalidArgumentException on padding error
*/
function unpad(string $paddedMessage): string
{
$pad = ord($paddedMessage[strlen($paddedMessage) - 1]);
if ($pad !== substr_count(substr($paddedMessage, -$pad), chr($pad))) {
throw new InvalidArgumentException('Padding error');
}

return substr($paddedMessage, 0, -$pad);
}
27 changes: 0 additions & 27 deletions src/EncoderInterface.php

This file was deleted.

72 changes: 0 additions & 72 deletions src/Fernet.php

This file was deleted.

Loading

0 comments on commit 757e83e

Please sign in to comment.