public function signMessage($privatekey, $message) { /** * Test code: * * $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); * extract($rsa->createKey()); * $spotSigning = new SpotSigning(); * $x = $spotSigning->signMessage($privatekey, 'testmessage'); * var_dump($x); * var_dump($spotSigning->checkRsaSignature('testmessage', $x['signature'], $x['publickey'], false)); * */ if (empty($privatekey)) { throw new InvalidPrivateKeyException(); } # if $rsa = new Crypt_RSA(); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $rsa->loadKey($privatekey); # extract de public key $signature = $rsa->sign($message); $publickey = $rsa->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW); return array('signature' => base64_encode($signature), 'publickey' => array('modulo' => base64_encode($publickey['n']->toBytes()), 'exponent' => base64_encode($publickey['e']->toBytes())), 'message' => $message); }
/** * Generate base64-encoded signature for the given byte string * using our private key. * * @param string $bytes as raw byte string * @return string base64url-encoded signature */ public function sign($bytes) { $sig = $this->privateKey->sign($bytes); if ($sig === false) { throw new ServerException('Could not sign data'); } return Magicsig::base64_url_encode($sig); }
function activateKeyGen($key, $hwid, $privKey, $offline = false) { $rsa = new Crypt_RSA(); $rsa->loadKey($privKey); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $rsa->setHash('sha512'); $activateKey = $rsa->sign(sha1(base64_decode($key) . hash('sha512', $hwid, true))); if ($offline) { $license = '------BEGIN ACTIVATION KEY------' . "\r\n"; $license .= chunk_split(base64_encode($activateKey)); $license .= '------END ACTIVATION KEY------'; } else { $license = base64_encode($activateKey); } return $license; }
public static function CreateLicense($licensee, $type) { // Gleiche Generalisierung wie am Client: $licenseeGen = self::GeneralizeDataString($licensee); $dataStr = $licenseeGen . (int) $type; // "ERIKAMUSTERMANN2" $rsa = new Crypt_RSA(); // Neue RSA-Klasse erstellen // Setzen der RSA-Optionen auf die, die auch am Client verwendet werden: $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_XML); $rsa->setHash('SHA1'); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); // privaten Schlüssel laden $rsa->loadKey(self::privateKey); // Erstellen der Signatur $signature = $rsa->sign($dataStr); // Formatierte Lizenzdaten zurückgeben return self::FormatLicense($licensee, $type, $signature); }
TYPE (0-блок, 1-тр-я) FF (256) BLOCK_ID FF FF FF FF (4 294 967 295) TIME FF FF FF FF (4 294 967 295) USER_ID FF FF FF FF FF (1 099 511 627 775) LEVEL FF (256) SIGN от 128 байта до 512 байт. Подпись от TYPE, BLOCK_ID, PREV_BLOCK_HASH, TIME, USER_ID, LEVEL, MRKL_ROOT Далее - тело блока (Тр-ии) */ // подписываем нашим нод-ключем заголовок блока $rsa = new Crypt_RSA(); $rsa->loadKey($node_private_key); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); //$rsa->setHash('sha256'); $for_sign = "0,{$new_block_id},{$testBlock->prev_block['hash']},{$time},{$my_user_id},{$testBlock->level},{$mrkl_root}"; debug_print('$for_sign=' . $for_sign, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $signature = $rsa->sign($for_sign); unset($rsa); list(, $signature_hex) = unpack("H*", $signature); debug_print('$signature_hex = ' . $signature_hex, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); // хэш шапки блока. нужен для сравнивания с другими и у кого будет меньше - у того блок круче $header_hash = ParseData::dsha256("{$my_user_id},{$new_block_id},{$prev_head_hash}"); debug_print("header_hash={$header_hash}", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $data = "{$new_block_id}\t{$time}\t{$testBlock->level}\t{$my_user_id}\t{$header_hash}\t{$signature_hex}\t{$mrkl_root}"; debug_print($data, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $file = save_tmp_644('FTB', $data); // для тестов получим что там есть $tmp_testblock_data = $db->query(__FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__, "\n\t\t\tSELECT *\n\t\t\tFROM `" . DB_PREFIX . "testblock`\n\t\t\t", 'fetch_array'); debug_print($tmp_testblock_data, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); debug_print("LOAD DATA LOCAL INFILE '{$file}' REPLACE INTO TABLE `" . DB_PREFIX . "testblock`", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); // т.к. эти данные создали мы сами, то пишем их сразу в таблицу проверенных данных, которые будут отправлены другим нодам $db->query(__FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__, "\n\t\t\tLOAD DATA LOCAL INFILE '{$file}' REPLACE INTO TABLE `" . DB_PREFIX . "testblock`\n\t\t\tFIELDS TERMINATED BY '\t'\n\t\t\t(`block_id`,`time`,`level`,`user_id`, @header_hash, @signature, @mrkl_root)\n\t\t\tSET `header_hash` = UNHEX(@header_hash),\n\t\t\t\t `signature` = UNHEX(@signature),\n\t\t\t\t `mrkl_root` = UNHEX(@mrkl_root)\n\t\t\t");
/** * Generate base64-encoded signature for the given byte string * using our private key. * * @param string $bytes as raw byte string * @return string base64-encoded signature */ public function sign($bytes) { $sig = $this->privateKey->sign($bytes); return Magicsig::base64_url_encode($sig); }
public function signature_for_message($message, $use_key = false) { $hash_algorithm = 'sha256'; // Sign with the private (local) key if (!$use_key) { if (!$this->key_local) { throw new Exception('No signing key has been set'); } $use_key = $this->key_local; } $this->ensure_crypto_loaded(); $rsa = new Crypt_RSA(); $rsa->loadKey($use_key); // This is the older signature mode; phpseclib's default is the preferred CRYPT_RSA_SIGNATURE_PSS; however, Forge JS doesn't yet support this. More info: https://en.wikipedia.org/wiki/PKCS_1 $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); // Don't do this: Crypt_RSA::sign() already calculates the digest of the hash // $hash = new Crypt_Hash($hash_algorithm); // $hashed = $hash->hash($message); // if ($this->debug) $this->log("Message hash (hash=$hash_algorithm) (hex): ".bin2hex($hashed)); // phpseclib defaults to SHA1 $rsa->setHash($hash_algorithm); $encrypted = $rsa->sign($message); if ($this->debug) { $this->log('Signed hash (mode=' . CRYPT_RSA_SIGNATURE_PKCS1 . ') (hex): ' . bin2hex($encrypted)); } $signature = base64_encode($encrypted); if ($this->debug) { $this->log("Message signature (base64): {$signature}"); } return $signature; }
main_unlock(); exit; } debug_print($max_other_currencies_votes, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $total_count_currencies = $db->query(__FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__, "\n\t\t\tSELECT count(`id`)\n\t\t\tFROM `" . DB_PREFIX . "currency`\n\t\t\t", 'fetch_one'); foreach ($max_other_currencies_votes as $currency_id => $count_and_votes) { $new_max_other_currencies[$currency_id] = get_max_vote($count_and_votes, 0, $total_count_currencies, 10); } if (get_community_users($db)) { $my_prefix = $testBlock->user_id . '_'; } else { $my_prefix = ''; } $node_private_key = get_node_private_key($db, $my_prefix); $json_data = json_encode($new_max_other_currencies); // подписываем нашим нод-ключем данные транзакции $data_for_sign = ParseData::findType('new_max_other_currencies') . ",{$time},{$my_user_id},{$json_data}"; $rsa = new Crypt_RSA(); $rsa->loadKey($node_private_key); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $signature = $rsa->sign($data_for_sign); debug_print('$data_for_sign=' . $data_for_sign . "\n", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); // создаем тр-ию. пишем $block_id, на момент которого были актуальны голоса в табле 'pct' $data = dec_binary(ParseData::findType('new_max_other_currencies'), 1) . dec_binary($time, 4) . ParseData::encode_length_plus_data($my_user_id) . ParseData::encode_length_plus_data($json_data) . ParseData::encode_length_plus_data($signature); $hash = ParseData::dsha256($data); insert_tx($data, $db); $new_tx_data['data'] = $data; $new_tx_data['hash'] = hextobin(md5($data)); tx_parser($new_tx_data, true); } main_unlock();
/** * Sign a message using a private RSA key * * @param string $payload The message to be signed * @param string $private_key An RSA private key * @return string A base64-encoded and url-encoded hash of the $payload_string */ private function signMessage($payload, $private_key) { $signature_urlencoded = ''; $rsa_signature = new \Crypt_RSA(); $rsa_signature->loadKey($private_key); $rsa_signature->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $rsa_signature->setHash('md5'); $payload_base64 = urldecode(substr($payload, 0, -6)); $signature_binary = $rsa_signature->sign($payload_base64); $signature_base64 = base64_encode($signature_binary); $signature_urlencoded = urlencode($signature_base64) . "decode"; return $signature_urlencoded; }
public function createSignature($text) { $rsa = new Crypt_RSA(); $rsa->loadKey($this['private_key']); return $rsa->sign($text); }
function encrypt_and_sign($pass, $encrypt_private_key, $data_for_sign) { debug_print("pass={$pass}", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); debug_print("encrypt_private_key={$encrypt_private_key}", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); debug_print("data_for_sign={$data_for_sign}", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); if ($pass !== '') { debug_print("pass exists", __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $aes = new Crypt_AES(CRYPT_AES_MODE_ECB); $aes = new Crypt_AES(CRYPT_AES_MODE_ECB); $aes->setKey(md5($pass)); $user_private_key = $aes->decrypt($encrypt_private_key); unset($aes); } else { $user_private_key = $encrypt_private_key; } debug_print("user_private_key=" . $user_private_key, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); $rsa = new Crypt_RSA(); $rsa->loadKey($user_private_key); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $signature = $rsa->sign($data_for_sign); unset($rsa); debug_print("signature=" . $signature, __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__); return $signature; }
include 'Crypt/RSA.php'; $rsa = new Crypt_RSA(); $msg = ""; $color = ""; $cisti_tekst = file_get_contents('./cisti_tekst.txt', FILE_USE_INCLUDE_PATH); if (isset($_POST['potpisi'])) { file_put_contents('potpis.txt', ''); $cisti_tekst = file_get_contents('./cisti_tekst.txt', FILE_USE_INCLUDE_PATH); $rsa->setHash("sha256"); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $privatekey = file_get_contents('./privatni_kljuc.txt', FILE_USE_INCLUDE_PATH); $rsa->loadKey($privatekey); //$signature = base64_encode($rsa->sign(base64_decode($cisti_tekst))); //$signature = base64_encode($rsa->sign(base64_decode($cisti_tekst))); $signature = $rsa->sign($cisti_tekst); $signature = base64_encode($signature); $ret = file_put_contents('potpis.txt', $signature, FILE_APPEND | LOCK_EX); if ($ret === false) { die('Neuspješna pohrana u datoteku'); } else { echo "U datoteku je pohranjeno: " . $ret . " bajtova"; } } if (isset($_POST['provjeri'])) { $rsa->setHash("sha256"); $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $publickey = file_get_contents('./javni_kljuc.txt', FILE_USE_INCLUDE_PATH); $rsa->loadKey($publickey); $signature = file_get_contents('./potpis.txt', FILE_USE_INCLUDE_PATH); $signature = base64_decode($signature);