-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yzwyzwyzw1 <1957855254@qq.com>
- Loading branch information
1 parent
2e84520
commit 601ddb0
Showing
6 changed files
with
101 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package sm4 | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"io/ioutil" | ||
) | ||
|
||
func ReadKeyFromMem(data []byte, pwd []byte) (SM4Key, error) { | ||
block, _ := pem.Decode(data) | ||
if block == nil { | ||
return nil, errors.New("SM4: pem decode failed") | ||
} | ||
if x509.IsEncryptedPEMBlock(block) { | ||
if block.Type != "SM4 ENCRYPTED KEY" { | ||
return nil, errors.New("SM4: unknown type") | ||
} | ||
if pwd == nil { | ||
return nil, errors.New("SM4: need passwd") | ||
} | ||
data, err := x509.DecryptPEMBlock(block, pwd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} | ||
if block.Type != "SM4 KEY" { | ||
return nil, errors.New("SM4: unknown type") | ||
} | ||
return block.Bytes, nil | ||
} | ||
|
||
func ReadKeyFromPem(FileName string, pwd []byte) (SM4Key, error) { | ||
data, err := ioutil.ReadFile(FileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ReadKeyFromMem(data, pwd) | ||
} | ||
|
||
func WriteKeytoMem(key SM4Key, pwd []byte) ([]byte, error) { | ||
if pwd != nil { | ||
block, err := x509.EncryptPEMBlock(rand.Reader, | ||
"SM4 ENCRYPTED KEY", key, pwd, x509.PEMCipherAES256) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pem.EncodeToMemory(block), nil | ||
} else { | ||
block := &pem.Block{ | ||
Type: "SM4 KEY", | ||
Bytes: key, | ||
} | ||
return pem.EncodeToMemory(block), nil | ||
} | ||
} | ||
|
||
func WriteKeyToPem(FileName string, key SM4Key, pwd []byte) error { | ||
pemBytes, err := WriteKeytoMem(key, pwd) | ||
if err != nil { | ||
return err | ||
} | ||
err = ioutil.WriteFile(FileName, pemBytes, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |