Example #1
0
<?php

if (!extension_loaded("libsodium")) {
    exit(0);
}
include "../autoload.php";
$salt = Salt::instance();
$keys = crypto_sign_keypair();
$sodium_sk = crypto_sign_secretkey($keys);
$sodium_pk = crypto_sign_publickey($keys);
$sk = FieldElement::fromString($sodium_sk);
$pk = FieldElement::fromString($sodium_pk);
$msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$sodium_sm = crypto_sign($msg, $sodium_sk);
$signed_msg = $salt->crypto_sign($msg, strlen($msg), $sk);
if (sodium_memcmp($sodium_sm, $signed_msg->toString()) === 0) {
    echo $sodium_sm . "\n";
    echo $signed_msg->toString() . "\n\n";
} else {
    echo "invalid signed message";
    exit(0);
}
$sodium_open_msg = crypto_sign_open($sodium_sm, $sodium_pk);
$plaintext = $salt->crypto_sign_open($signed_msg, count($signed_msg), $pk);
if ($plaintext === false) {
    echo "debug time...";
    exit(0);
}
if (sodium_memcmp($sodium_open_msg, $plaintext->toString()) === 0) {
    echo $sodium_open_msg . "\n";
    echo $plaintext->toString() . "\n";
Example #2
0
File: Salt.php Project: rugk/Salt
 /**
  * Get bytes presentation from a value.
  *
  * @param  mixed
  * @return FieldElement
  */
 public static function decodeInput($value)
 {
     if (is_string($value)) {
         return FieldElement::fromString($value);
     } else {
         if ($value instanceof FieldElement) {
             return $value;
         } else {
             if ($value instanceof SplFixedArray) {
                 return FieldElement::fromArray($value->toArray(), false);
             } else {
                 if ((array) $value === $value) {
                     return FieldElement::fromArray($value, false);
                 }
             }
         }
     }
     throw new SaltException('Unexpected input');
 }
Example #3
0
 public static function fromBase64($base64)
 {
     return FieldElement::fromString(base64_decode($base64, true));
 }
 /**
  * Derive the public key
  *
  * @param string $privateKey in binary
  * @return string public key as binary
  */
 public final function derivePublicKey($privateKey)
 {
     $privateKeyElement = \FieldElement::fromString($privateKey);
     return Salt::instance()->crypto_scalarmult_base($privateKeyElement)->toString();
 }