Crypto.php 814 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. class EcdsaCrypto {
  3. private $binPath;
  4. public function __construct($binPath){
  5. $this->binPath = $binPath;
  6. }
  7. /**
  8. * @param string $privtKey
  9. * @param string $msg
  10. * @return string
  11. */
  12. public function signEcdsa($privtKey, $msg) {
  13. $privtKey = str_replace('"', '\\"', $privtKey);
  14. $sign = exec($this->binPath . ' sign ecsda -k "' . $privtKey . '" -m "' . $msg . '" -f std');
  15. return trim($sign);
  16. }
  17. }
  18. /**
  19. * 目前仅支持pem格式的私钥签名
  20. * @param string $pemPrivtKey
  21. * @param string $oriMsg
  22. * @return string
  23. */
  24. function sign_ecdsa($pemPrivtKey, $oriMsg) {
  25. $key = openssl_pkey_get_private($pemPrivtKey);
  26. openssl_sign($oriMsg, $sign, $key, OPENSSL_ALGO_SHA256);
  27. openssl_free_key($key);
  28. return bin2hex($sign);
  29. }