aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Clerc <jclerc@google.com>2015-09-11 22:37:47 +0200
committerJeremy Clerc <jeremy@clerc.io>2015-09-11 22:37:47 +0200
commitdf6776142e8974dad88294498847094c903f4164 (patch)
tree4217ab8fc9fc005b5df4f2be016f58ea28030cee
parent588a6d83106c86b3c9ff718c66846d74531ae8e4 (diff)
downloadeasypki-df6776142e8974dad88294498847094c903f4164.tar.xz
Move NextNumber to easyca.go
-rw-r--r--pkg/easyca/easyca.go38
-rw-r--r--pkg/easyca/serial.go46
2 files changed, 38 insertions, 46 deletions
diff --git a/pkg/easyca/easyca.go b/pkg/easyca/easyca.go
index 38d1878..92f950a 100644
--- a/pkg/easyca/easyca.go
+++ b/pkg/easyca/easyca.go
@@ -404,3 +404,41 @@ func GeneratePKIStructure(pkiroot string) error {
}
return nil
}
+
+func NextNumber(pkiroot, name string) (*big.Int, error) {
+ serial := big.NewInt(0)
+
+ f, err := os.OpenFile(filepath.Join(pkiroot, name), os.O_RDWR, 0644)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ n, err := fmt.Fscanf(f, "%X\n", serial)
+ if err != nil {
+ return nil, err
+ }
+ if n != 1 {
+ return nil, fmt.Errorf("supposed to read 1 element, read: %v", n)
+ }
+
+ next := big.NewInt(1)
+ next.Add(serial, next)
+ output := fmt.Sprintf("%X", next)
+ // For compatibility with openssl we need an even length
+ if len(output)%2 == 1 {
+ output = "0" + output
+ }
+ f.Truncate(0)
+ f.Seek(0, 0)
+
+ n, err = fmt.Fprintln(f, output)
+ if err != nil {
+ return nil, err
+ }
+ if n == 0 {
+ return nil, fmt.Errorf("supposed to write 1 element, written: %v", n)
+ }
+
+ return serial, nil
+}
diff --git a/pkg/easyca/serial.go b/pkg/easyca/serial.go
deleted file mode 100644
index fc8d187..0000000
--- a/pkg/easyca/serial.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package easyca
-
-import (
- "fmt"
- "math/big"
- "os"
- "path/filepath"
-)
-
-func NextNumber(pkiroot, name string) (*big.Int, error) {
- serial := big.NewInt(0)
-
- f, err := os.OpenFile(filepath.Join(pkiroot, name), os.O_RDWR, 0644)
- if err != nil {
- return nil, err
- }
- defer f.Close()
-
- n, err := fmt.Fscanf(f, "%X\n", serial)
- if err != nil {
- return nil, err
- }
- if n != 1 {
- return nil, fmt.Errorf("supposed to read 1 element, read: %v", n)
- }
-
- next := big.NewInt(1)
- next.Add(serial, next)
- output := fmt.Sprintf("%X", next)
- // For compatibility with openssl we need an even length
- if len(output)%2 == 1 {
- output = "0" + output
- }
- f.Truncate(0)
- f.Seek(0, 0)
-
- n, err = fmt.Fprintln(f, output)
- if err != nil {
- return nil, err
- }
- if n == 0 {
- return nil, fmt.Errorf("supposed to write 1 element, written: %v", n)
- }
-
- return serial, nil
-}