-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: add comments for exported functions
- Loading branch information
Jemmy
committed
Jan 20, 2021
1 parent
0c926e5
commit b16d9bd
Showing
17 changed files
with
45 additions
and
70 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
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 |
---|---|---|
|
@@ -6,14 +6,14 @@ import ( | |
"os" | ||
"os/user" | ||
|
||
"github.com/jemmyh/terminal_note/note" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/AlecAivazis/survey.v1" | ||
"hujm.net/terminal_note/note" | ||
) | ||
|
||
/* | ||
* @CreateTime: 2021/1/12 20:59 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
@@ -31,13 +31,14 @@ var rootCmd = &cobra.Command{ | |
Long: `Terminal Notebook is CLI App, which is implemented by Golang.`, | ||
} | ||
|
||
// Execute ... | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
// GetUserName ... | ||
// GetUserName returns the current logined user, the same as `whoami` | ||
func GetUserName() string { | ||
if userName == "" { | ||
u, _ := user.Current() | ||
|
@@ -46,13 +47,15 @@ func GetUserName() string { | |
return userName | ||
} | ||
|
||
// CheckNotebook make app exit if user's notebook does not exist. | ||
func CheckNotebook(userName string) { | ||
if !note.CheckDbFileExist(userName) { | ||
fmt.Printf("No existing notebook found for %s. Use `create` to create one.\n", userName) | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
// GetInputPassword gets password from stdin. | ||
func GetInputPassword() string { | ||
password := "" | ||
prompt := &survey.Password{Message: "Please type your password:"} | ||
|
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import "github.com/jemmyh/terminal_note/cmd" | |
|
||
/* | ||
* @CreateTime: 2021/1/14 15:31 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ import ( | |
/* | ||
* @CreateTime: 2021/1/12 18:28 | ||
* @Author: Jemmy@[email protected] | ||
* @Description: | ||
* @Description: note is a message in notebook | ||
*/ | ||
|
||
// Note means a diary note | ||
|
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ import ( | |
/* | ||
* @CreateTime: 2021/1/12 18:30 | ||
* @Author: Jemmy@[email protected] | ||
* @Description: | ||
* @Description: notebook | ||
*/ | ||
|
||
// NoteBook represents a diary book that contains many diary notes. | ||
|
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
|
||
/* | ||
* @CreateTime: 2021/1/13 11:41 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
|
||
/* | ||
* @CreateTime: 2021/1/12 20:49 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: a iterator for note | ||
*/ | ||
|
||
|
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import "testing" | |
|
||
/* | ||
* @CreateTime: 2021/1/13 16:09 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
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 |
---|---|---|
|
@@ -3,13 +3,11 @@ package note | |
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
/* | ||
* @CreateTime: 2021/1/13 11:31 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
@@ -23,17 +21,3 @@ func TestNewNote(t *testing.T) { | |
note = NewNote("test content") | ||
t.Log(note.String()) | ||
} | ||
|
||
func TestNote_Serialize(t *testing.T) { | ||
data := note.Serialize() | ||
t.Logf("data: %s", data) | ||
|
||
newNote := DeserializeNote(data) | ||
t.Log(newNote.String()) | ||
assert.Equal(t, note.String(), newNote.String()) | ||
} | ||
|
||
func TestGetHeadNote(t *testing.T) { | ||
head := GetHeadNote() | ||
t.Log(head.String()) | ||
} |
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 |
---|---|---|
|
@@ -9,34 +9,34 @@ import ( | |
|
||
/* | ||
* @CreateTime: 2021/1/13 18:58 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: rsa | ||
1. GenRsaKey 得到公私钥 | ||
2. RsaEncrypt 传入要加密的数据 和 公钥,进行加密,得到加密后的数据 | ||
3. RsaDecrypt 传入加密后的数据和私钥解密,得到解密后的数据 | ||
1. GenRsaKey returns publicKey and privateKey | ||
2. RsaEncrypt input data needed to encrypt and publicKey, output data that has been encrypted | ||
3. RsaDecrypt input encrypted data and privateKey, output the decrypted data | ||
*/ | ||
|
||
// RSAGenKey generate rsa key pair. | ||
func RSAGenKey(bits int) (pubKey, prvKey []byte, err error) { | ||
/* | ||
生成私钥 | ||
generate privateKey | ||
*/ | ||
// 1、使用RSA中的GenerateKey方法生成私钥 | ||
// 1、get a privateKey | ||
privateKey, err := rsa.GenerateKey(rand.Reader, bits) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
// 2、通过X509标准将得到的RAS私钥序列化为:ASN.1 的DER编码字符串 | ||
// 2、Marshal the privateKey | ||
privateStream := x509.MarshalPKCS1PrivateKey(privateKey) | ||
// 3、将私钥字符串设置到pem格式块中 | ||
// 3、put the marshaled privateKey into a Block | ||
block1 := &pem.Block{ | ||
Type: "private key", | ||
Bytes: privateStream, | ||
} | ||
prvKey = pem.EncodeToMemory(block1) | ||
|
||
/* | ||
生成公钥 | ||
genarate publicKey from privateKey | ||
*/ | ||
publicKey := privateKey.PublicKey | ||
publicStream, err := x509.MarshalPKIXPublicKey(&publicKey) | ||
|
@@ -52,24 +52,24 @@ func RSAGenKey(bits int) (pubKey, prvKey []byte, err error) { | |
func RSAEncrypt(src []byte, pubKey []byte) (res []byte, err error) { | ||
block, _ := pem.Decode(pubKey) | ||
|
||
// 使用X509将解码之后的数据 解析出来 | ||
// unmarshal publicKey | ||
keyInit, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return | ||
} | ||
publicKey := keyInit.(*rsa.PublicKey) | ||
// 使用公钥加密数据 | ||
// encrypt data with publicKey | ||
res, err = rsa.EncryptPKCS1v15(rand.Reader, publicKey, src) | ||
return | ||
} | ||
|
||
// RSADecrypt decrypt encrypted data with private key. | ||
func RSADecrypt(encryptedSrc []byte, prvKey []byte) (res []byte, err error) { | ||
// 解码 | ||
// decode the privateKey | ||
block, _ := pem.Decode(prvKey) | ||
blockBytes := block.Bytes | ||
privateKey, err := x509.ParsePKCS1PrivateKey(blockBytes) | ||
// 还原数据 | ||
// decrypt by privateKey | ||
res, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedSrc) | ||
return | ||
} | ||
|
@@ -78,21 +78,18 @@ func RSADecrypt(encryptedSrc []byte, prvKey []byte) (res []byte, err error) { | |
// RSAGenKeyWithPwd generate rsa pair key with specified password | ||
func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { | ||
/* | ||
生成私钥 | ||
generate privateKey | ||
*/ | ||
// 1、使用RSA中的GenerateKey方法生成私钥 | ||
privateKey, err := rsa.GenerateKey(rand.Reader, bits) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
// 2、通过X509标准将得到的RAS私钥序列化为:ASN.1 的DER编码字符串 | ||
privateStream := x509.MarshalPKCS1PrivateKey(privateKey) | ||
// 3、将私钥字符串设置到pem格式块中 | ||
block1 := &pem.Block{ | ||
Type: "private key", | ||
Bytes: privateStream, | ||
} | ||
// 通过自定义密码加密 | ||
// use optional password | ||
if pwd != "" { | ||
block1, err = x509.EncryptPEMBlock(rand.Reader, block1.Type, block1.Bytes, []byte(pwd), x509.PEMCipherAES256) | ||
if err != nil { | ||
|
@@ -102,7 +99,7 @@ func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { | |
prvKey = pem.EncodeToMemory(block1) | ||
|
||
/* | ||
生成公钥 | ||
generate publuicKey from privateKey | ||
*/ | ||
publicKey := privateKey.PublicKey | ||
publicStream, err := x509.MarshalPKIXPublicKey(&publicKey) | ||
|
@@ -116,7 +113,6 @@ func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { | |
|
||
// RSADecryptWithPwd decrypt src with private key and password | ||
func RSADecryptWithPwd(src []byte, prvKey []byte, pwd string) (res []byte, err error) { | ||
// 解码 | ||
block, _ := pem.Decode(prvKey) | ||
blockBytes := block.Bytes | ||
if pwd != "" { | ||
|
@@ -126,7 +122,6 @@ func RSADecryptWithPwd(src []byte, prvKey []byte, pwd string) (res []byte, err e | |
} | ||
} | ||
privateKey, err := x509.ParsePKCS1PrivateKey(blockBytes) | ||
// 还原数据 | ||
res, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, src) | ||
return | ||
} |
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
|
||
/* | ||
* @CreateTime: 2021/1/13 19:46 | ||
* @Author: hujiaming | ||
* @Author: JemmyHu <[email protected]> | ||
* @Description: | ||
*/ | ||
|
||
|
Oops, something went wrong.