aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Keyutils.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-03-08 09:31:23 +0000
committerMario <mario@mariovavti.com>2021-03-08 09:31:23 +0000
commit951e9c8c4f39dd8577834d5bc501c05d80722de9 (patch)
tree6a42c6a229c7b2b3e13bc5af77a733d054242695 /Zotlabs/Lib/Keyutils.php
parentf94b046333c57acde493ee5dc2511acc6baca701 (diff)
parent89415e17313578eb115c441480b6e0ddfa90afef (diff)
downloadvolse-hubzilla-951e9c8c4f39dd8577834d5bc501c05d80722de9.tar.gz
volse-hubzilla-951e9c8c4f39dd8577834d5bc501c05d80722de9.tar.bz2
volse-hubzilla-951e9c8c4f39dd8577834d5bc501c05d80722de9.zip
Merge branch '5.4RC'5.4
Diffstat (limited to 'Zotlabs/Lib/Keyutils.php')
-rw-r--r--Zotlabs/Lib/Keyutils.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/Zotlabs/Lib/Keyutils.php b/Zotlabs/Lib/Keyutils.php
new file mode 100644
index 000000000..616ecfcf6
--- /dev/null
+++ b/Zotlabs/Lib/Keyutils.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Zotlabs\Lib;
+
+use phpseclib\Crypt\RSA;
+use phpseclib\Math\BigInteger;
+
+/**
+ * Keyutils
+ * Convert RSA keys between various formats
+ */
+class Keyutils {
+
+ /**
+ * @param string $m modulo
+ * @param string $e exponent
+ * @return string
+ */
+ public static function meToPem($m, $e) {
+
+ $rsa = new RSA();
+ $rsa->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();
+
+ $m = $rsa->modulus->toBytes();
+ $e = $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