Back

private-key? (clj)

(source)

function

(private-key? k)
Return true if key `k` is a private key.

Examples

funcool/buddy-core
(ns buddy.core.keys-tests
  (:require [clojure.test :refer :all]
            [buddy.core.codecs :refer :all]
            [buddy.core.nonce :as nonce]
            [buddy.core.bytes :as bytes]
            [buddy.core.keys :as keys]))

  (testing "Read ecdsa priv key"
    (let [pkey (keys/private-key "test/_files/privkey.ecdsa.pem" "secret")]
      (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey))
      (is (keys/private-key? pkey))))

  (testing "Read ecdsa priv key from string."
    (let [keystr (slurp "test/_files/privkey.ecdsa.pem")
          pkey (keys/str->private-key keystr)]
      (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey))
      (is (keys/private-key? pkey))))

  (testing "Read rsa priv key from string"
    (let [keystr (slurp "test/_files/privkey.3des.rsa.pem")
          pkey (keys/str->private-key keystr "secret")]
      (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey))
      (is (keys/private-key? pkey))))

  (testing "Read rsa priv key from string without password."
    (is (thrown? clojure.lang.ExceptionInfo
                 (let [keystr (slurp "test/_files/privkey.3des.rsa.pem")
                       pkey (keys/str->private-key keystr)])))
    (let [keystr (slurp "test/_files/privkey.rsa.pem")
          pkey (keys/str->private-key keystr)]
      (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey))
      (is (keys/private-key? pkey))))