pterotype/includes/pgp.php

53 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2018-10-04 10:44:32 +00:00
<?php
2018-10-21 02:34:20 +00:00
namespace pterotype\pgp;
2018-10-04 10:44:32 +00:00
function gen_key( $actor_slug ) {
$rsa = new \phpseclib\Crypt\RSA();
return $rsa->createKey( 2048 );
}
function persist_key( $actor_id, $public_key, $private_key ) {
global $wpdb;
return $wpdb->replace(
$wpdb->prefix . 'pterotype_keys',
2018-10-04 10:44:32 +00:00
array(
'actor_id' => $actor_id,
'public_key' => $public_key,
'private_key' => $private_key
),
array( '%d', '%s', '%s' )
);
}
2018-10-06 12:01:50 +00:00
function sign_data( $data, $actor_id ) {
$secret_key = get_private_key( $actor_id );
$sig = null;
openssl_sign( $data, $sig, $secret_key, OPENSSL_ALGO_SHA256 );
2018-10-06 12:01:50 +00:00
if ( ! $sig ) {
return new \WP_Error(
'pgp_error',
__( 'Unable to sign data', 'pterotype' )
);
}
return base64_encode( $sig );
2018-10-06 12:01:50 +00:00
}
2018-10-04 10:44:32 +00:00
function get_public_key( $actor_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare(
"SELECT public_key FROM {$wpdb->prefix}pterotype_keys WHERE actor_id = %d",
$actor_id
2018-10-04 10:44:32 +00:00
) );
}
2018-10-06 12:01:50 +00:00
function get_private_key( $actor_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare(
"
SELECT private_key FROM {$wpdb->prefix}pterotype_keys WHERE actor_id = %d
",
$actor_id
2018-10-06 12:01:50 +00:00
) );
}
2018-10-04 10:44:32 +00:00
?>