Skip to content

Commit

Permalink
feat: rename key funcs (#13)
Browse files Browse the repository at this point in the history
* rename key funcs

* rename key funcs
  • Loading branch information
shipengqi committed Sep 18, 2022
1 parent 964c1c9 commit 806d5ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 24 additions & 6 deletions crtutil/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,47 @@ import (
)

// ParseKeyFile parses an unencrypted crypto.PrivateKey from the given file.
// Deprecated: use ReadFileAsSigner instead.
func ParseKeyFile(fpath string) (crypto.PrivateKey, error) {
return ReadFileAsSigner(fpath)
}

// ReadFileAsSigner read a crypto.PrivateKey from the given file.
func ReadFileAsSigner(fpath string) (crypto.PrivateKey, error) {
f, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
return ParseKeyBytes(f, false)
return ReadBytesAsSigner(f, false)
}

// ParseKeyFileWithPass parses an unencrypted crypto.PrivateKey from the given file.
// ParseKeyFileWithPass read a crypto.PrivateKey from the given file.
// Deprecated: use ReadFileAsSignerWithPass instead.
func ParseKeyFileWithPass(keyPath, keyPass string) (crypto.PrivateKey, error) {
return ReadFileAsSignerWithPass(keyPath, keyPass)
}

// ReadFileAsSignerWithPass read a crypto.PrivateKey from the given file.
func ReadFileAsSignerWithPass(keyPath, keyPass string) (crypto.PrivateKey, error) {
f, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
return parseKeyBytes(f, []byte(keyPass), false)
return readBytesAsSigner(f, []byte(keyPass), false)
}

// ParseKeyBytes parses an unencrypted crypto.PrivateKey from the given data.
// ParseKeyBytes read a crypto.PrivateKey from the given data.
// Deprecated: use ReadBytesAsSigner instead.
func ParseKeyBytes(data []byte, isBase64 bool) (crypto.PrivateKey, error) {
return parseKeyBytes(data, nil, isBase64)
return readBytesAsSigner(data, nil, isBase64)
}

// ReadBytesAsSigner read a crypto.PrivateKey from the given data.
func ReadBytesAsSigner(data []byte, isBase64 bool) (crypto.PrivateKey, error) {
return readBytesAsSigner(data, nil, isBase64)
}

func parseKeyBytes(key, keypass []byte, isBase64 bool) (crypto.PrivateKey, error) {
func readBytesAsSigner(key, keypass []byte, isBase64 bool) (crypto.PrivateKey, error) {
var err error
dkeystr := key

Expand Down
6 changes: 3 additions & 3 deletions crtutil/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

func TestParseKeyFile(t *testing.T) {
prik, err := ParseKeyFile("testdata/server-rsa.key")
prik, err := ReadFileAsSigner("testdata/server-rsa.key")
assert.NoError(t, err)

_, ok := prik.(*rsa.PrivateKey)
assert.True(t, ok)
}

func TestParseKeyFileWithPass(t *testing.T) {
prik, err := ParseKeyFileWithPass("testdata/server-rsa.key", "")
prik, err := ReadFileAsSignerWithPass("testdata/server-rsa.key", "")
assert.NoError(t, err)

_, ok := prik.(*rsa.PrivateKey)
Expand All @@ -28,7 +28,7 @@ func TestParseKeyBytes(t *testing.T) {
f, err := ioutil.ReadFile("testdata/server-rsa-base64.key")
assert.NoError(t, err)

prik, err := ParseKeyBytes(f, true)
prik, err := ReadBytesAsSigner(f, true)
assert.NoError(t, err)

_, ok := prik.(*rsa.PrivateKey)
Expand Down

0 comments on commit 806d5ee

Please sign in to comment.