anchor.diff:  - ECDSA keypair
   
   
  -=== RSA Keypair
  +==== RSA Keypair
   
   An RSA keypair is obviously used for RSA encryption/decryption, but it
   is also used for making digital signatures with RSA-derived
  
positive.code: = buddy-core - Cryptographic Api
  Andrey Antukh, <niwi@niwi.nz>
  1.4.0
  :toc: left
  :!numbered:
  :source-highlighter: pygments
  
negative.code: 
  # Generate a public key from private key
  openssl ec -in ecprivkey.pem -pubout -out ecpubkey.pem
  ----
  
  [[jwk]]
  === Json Web Key (JWK)
  
  A JSON Web Key (JWK) is a JavaScript Object Notation (JSON) data
  structure that represents a cryptographic key of different types.
  
  _buddy-sign_ provides functions for reading and saving JCA keys in JWK format
  
  Currently supported JWK key types are
  
  * RSA key pairs (No RSA-CRT support yet)
  * OKP key pairs (Ed25519)
  * EC key pairs (P-256, P-384, P-521 curves)
  
  Example of JWS signing for Ed25519 keys
  
  [source, clojure]
  ----
  (require '[buddy.core.keys :as keys])
  
  (def edkey {:kty "OKP",
              :crv "Ed25519",
              :d "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A",
              :x "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"})
  
  (def privkey (keys/jwk->private-key edkey))
  ----
  
  You can also convert from PEM to JWK like this
  
  [source, clojure]
  ----
  (require '[buddy.core.keys :as keys])
  
  (def prv (keys/private-key "private.pem"))
  (def pub (keys/public-key "public.pem"))
  
  ;; JWK requires both public and private keys for export
  (def jwk (keys/jwk prv pub))
  (def jwk-pub (keys/public-key->jwk pub))
  ----
  
  [[ed25519-generation]]You can generate and save keys in JWK format like this
  
  [source,clojure]
  ----
  (require '[buddy.core.keys :as keys])
  (import 'java.security.KeyPairGenerator)
  (import 'java.security.SecureRandom)
  
  (defn generate-keypair-ed25519
    []
    (let [kg (KeyPairGenerator/getInstance "EdDSA" "EdDSA")]
      (.initialize kg
                   256
                   ;; JDK8 only, use getInstance on JDK7 (make sure it's true random source)
                   (SecureRandom/getInstanceStrong))
      (.genKeyPair kg)))
  
  (let [pair (generate-keypair-ed25519)]
    (keys/jwk (.getPrivate pair) (.getPublic pair)))
  
  ;; =>
  ;; {:kty "OKP",
  ;;  :crv "Ed25519",
  ;;  :d "5q3yhCdSDMj9Za9jJE0vhfExlTV8JeSe6XnfblAFkPY",
  ;;  :x "JbbhB16SaghHiGHx3FutVMfVTgu9-SCtZGfZyoDZSbQ"}
  ----
  
  You can also calculate JWK thumbprint using `jwk-thumbprint` function
  
  [source,clojure]
  ----
  (require '[buddy.core.keys :as keys])
  (require '[buddy.core.codecs :as codecs])
  
  (def edkey {:kty "OKP",
              :crv "Ed25519",
              :d "nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A",
              :x "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"})
  
  (-> (keys/jwk-thumbprint edkey)
      (codecs/bytes->hex))
  
  ;; => "90facafea9b1556698540f70c0117a22ea37bd5cf3ed3c47093c1707282b4b89"
  ----
  
  
  