-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup, improve crypto and hash functions
- Loading branch information
Showing
11 changed files
with
136 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ | |
|
||
class CertificateDecodingException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Olssonm\Swish\Util; | ||
|
||
use ArrayAccess; | ||
use Olssonm\Swish\Exceptions\CertificateDecodingException; | ||
|
||
class Crypto | ||
{ | ||
/** | ||
* Create a sha512 hash of the payload. | ||
* | ||
* @param string $payload | ||
* @return string | ||
*/ | ||
public static function hash(string $payload): string | ||
{ | ||
$bytes = mb_convert_encoding($payload, 'UTF-8'); | ||
$hash = hash('sha512', $bytes, true); | ||
return $hash; | ||
} | ||
|
||
/** | ||
* Sign a hash with a certificate. | ||
* | ||
* @param string $hash | ||
* @param string $certificate | ||
* @return string | ||
* @throws CertificateDecodingException | ||
*/ | ||
public static function sign(string $hash, string $certificate): string | ||
{ | ||
$signature = null; | ||
|
||
// Load your private key | ||
$key = file_get_contents($certificate); | ||
|
||
if (!$key) { | ||
throw new CertificateDecodingException('Failed to load certificate'); | ||
} | ||
|
||
$id = openssl_get_privatekey($key); | ||
|
||
// Sign the hash | ||
openssl_sign($hash, $signature, $id, OPENSSL_ALGO_SHA512); // @phpstan-ignore argument.type | ||
|
||
return $signature; | ||
} | ||
|
||
/** | ||
* Hash and sign a payload. | ||
* | ||
* @param ArrayAccess $payload | ||
* @param ?string $certificate | ||
* @return string | ||
*/ | ||
public static function hashAndSign(ArrayAccess $payload, ?string $certificate): string | ||
{ | ||
$data = json_encode($payload); | ||
|
||
if (!$data || !$certificate) { | ||
throw new CertificateDecodingException('Failed to encode payload'); | ||
} | ||
|
||
$hash = self::hash($data); | ||
$signature = self::sign($hash, $certificate); | ||
|
||
return base64_encode($signature); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters