From 9e226414252e02eb1340fdaf75f76f00d6ded77c Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Thu, 4 Feb 2021 22:20:55 +0100 Subject: add Lib/Keyutils --- Zotlabs/Lib/Keyutils.php | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Zotlabs/Lib/Keyutils.php (limited to 'Zotlabs/Lib/Keyutils.php') diff --git a/Zotlabs/Lib/Keyutils.php b/Zotlabs/Lib/Keyutils.php new file mode 100644 index 000000000..d4b660c04 --- /dev/null +++ b/Zotlabs/Lib/Keyutils.php @@ -0,0 +1,99 @@ +loadKey([ + 'e' => new BigInteger($e, 256), + 'n' => new BigInteger($m, 256) + ]); + return $rsa->getPublicKey(); + + } + + /** + * @param string key + * @return string + */ + public static function rsaToPem($key) { + + $rsa = new RSA(); + $rsa->setPublicKey($key); + + return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8); + + } + + /** + * @param string key + * @return string + */ + public static function pemToRsa($key) { + + $rsa = new RSA(); + $rsa->setPublicKey($key); + + return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1); + + } + + /** + * @param string $key key + * @param string $m reference modulo + * @param string $e reference exponent + */ + public static function pemToMe($key, &$m, &$e) { + + $rsa = new RSA(); + $rsa->loadKey($key); + $rsa->setPublicKey(); + + $e = $rsa->modulus->toBytes(); + $m = $rsa->exponent->toBytes(); + + } + + /** + * @param string $pubkey + * @return string + */ + public static function salmonKey($pubkey) { + self::pemToMe($pubkey, $m, $e); + return 'RSA' . '.' . base64url_encode($m, true) . '.' . base64url_encode($e, true); + } + + /** + * @param string $key + * @return string + */ + public static function convertSalmonKey($key) { + if (strstr($key, ',')) + $rawkey = substr($key, strpos($key, ',') + 1); + else + $rawkey = substr($key, 5); + + $key_info = explode('.', $rawkey); + + $m = base64url_decode($key_info[1]); + $e = base64url_decode($key_info[2]); + + return self::meToPem($m, $e); + } + +} \ No newline at end of file -- cgit v1.2.3 From d316d9436bde512f520c5d1615d107b8e74744df Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 8 Feb 2021 10:17:05 +0000 Subject: mixed up variables --- Zotlabs/Lib/Keyutils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Zotlabs/Lib/Keyutils.php') diff --git a/Zotlabs/Lib/Keyutils.php b/Zotlabs/Lib/Keyutils.php index d4b660c04..616ecfcf6 100644 --- a/Zotlabs/Lib/Keyutils.php +++ b/Zotlabs/Lib/Keyutils.php @@ -64,8 +64,8 @@ class Keyutils { $rsa->loadKey($key); $rsa->setPublicKey(); - $e = $rsa->modulus->toBytes(); - $m = $rsa->exponent->toBytes(); + $m = $rsa->modulus->toBytes(); + $e = $rsa->exponent->toBytes(); } -- cgit v1.2.3