aboutsummaryrefslogtreecommitdiff
path: root/rsa.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-15 21:04:59 +0100
committerGibheer <gibheer@gmail.com>2015-02-15 21:04:59 +0100
commitf9164f3f99edf4ae8343d6c67b915e788a5624f8 (patch)
treec3a7029eeb1392bcb73d3bbedec837ff46652591 /rsa.go
initial commit for pki
pki is a small library to make building some of the crypto stuff easier in go.
Diffstat (limited to 'rsa.go')
-rw-r--r--rsa.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/rsa.go b/rsa.go
new file mode 100644
index 0000000..48135b2
--- /dev/null
+++ b/rsa.go
@@ -0,0 +1,54 @@
+package pkilib
+
+import (
+ "crypto"
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/x509"
+ "errors"
+)
+
+type (
+ RsaPrivateKey struct {
+ private_key *rsa.PrivateKey
+ }
+
+ RsaPublicKey struct {
+ public_key rsa.PublicKey
+ }
+)
+
+// generate a new rsa private key
+func NewPrivateKeyRsa(size int) (*RsaPrivateKey, error) {
+ key, err := rsa.GenerateKey(rand.Reader, size)
+ if err != nil { return nil, err }
+ return &RsaPrivateKey{key}, nil
+}
+
+// load a rsa private key its ASN.1 presentation
+func LoadPrivateKeyRsa(raw []byte) (*RsaPrivateKey, error) {
+ key, err := x509.ParsePKCS1PrivateKey(raw)
+ if err != nil { return nil, err }
+ return &RsaPrivateKey{key}, nil
+}
+
+func (pr *RsaPrivateKey) Public() PublicKey {
+ return &RsaPublicKey{pr.private_key.Public().(rsa.PublicKey)}
+}
+
+func (pr RsaPrivateKey) Sign(message []byte) ([]byte, error) {
+ return make([]byte, 0), errors.New("not implemented yet!")
+}
+
+// get the private key
+func (pr RsaPrivateKey) privateKey() crypto.PrivateKey {
+ return pr.private_key
+}
+
+func (pr RsaPrivateKey) MarshalPem() (marshalledPemBlock, error) {
+ return nil, errors.New("not implemented yet!")
+}
+
+func (pu *RsaPublicKey) Verify(message []byte, signature []byte) (bool, error) {
+ return false, errors.New("not implemented yet!")
+}