Skip to content

Commit

Permalink
Fix: add comments for exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jemmy committed Jan 20, 2021
1 parent 0c926e5 commit b16d9bd
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# terminal_note
`terminal_note` is a terminal notebook app, in which you can record some notes in your terminal and set password for it.
`terminal_note` is a terminal notebook app with password, in which you can record some notes in your terminal.

## Install
```go
$ cd $GOPATH && git clone https://github.com/JemmyH/terminal_note.git
$ cd $GOPATH && mkdir jemmyh && cd jemmyh && git clone https://github.com/JemmyH/terminal_note.git
$ cd terminal_note && go build -o tnote && mv tnote $GOPATH/bin/
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License.
package cmd

import (
"github.com/jemmyh/terminal_note/note"
"github.com/spf13/cobra"
"hujm.net/terminal_note/note"
)

var content string
Expand Down
12 changes: 1 addition & 11 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd
import (
"fmt"

"github.com/jemmyh/terminal_note/note"
"github.com/spf13/cobra"
"hujm.net/terminal_note/note"
)

// createCmd represents the create command
Expand All @@ -39,14 +39,4 @@ var createCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(createCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License.
package cmd

import (
"github.com/jemmyh/terminal_note/note"
"github.com/spf13/cobra"
"hujm.net/terminal_note/note"
)

var (
Expand Down
10 changes: 5 additions & 5 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"

"github.com/boltdb/bolt"
"github.com/jemmyh/terminal_note/note"
"github.com/spf13/cobra"
"hujm.net/terminal_note/note"
)

var (
Expand Down Expand Up @@ -99,6 +99,7 @@ func (p *printStruct) checkBreak(id []byte) bool {
func (p *printStruct) printNote(n *note.Note) {
if p.verbose {
fmt.Println(n.String())
// TODO: use arrow
// if p.reverse && n.PrevID != nil {
// fmt.Println("⇧")
// fmt.Println("|")
Expand All @@ -115,11 +116,10 @@ func (p *printStruct) printNote(n *note.Note) {
var printCmd = &cobra.Command{
Use: "print",
Short: "Print notes in your notebook",
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(_ *cobra.Command, _ []string) {
CheckNotebook(GetUserName())
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("user: %s, myP:%+v \n", GetUserName(), myP)
Run: func(_ *cobra.Command, _ []string) {
userName := GetUserName()
passwd := GetInputPassword()
switch myP.printType {
Expand All @@ -144,7 +144,7 @@ var printCmd = &cobra.Command{
}
}
case "range":

// TODO: range print
default:
fmt.Println("invalid type: " + myP.printType)
return
Expand Down
9 changes: 6 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*/

Expand All @@ -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()
Expand All @@ -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:"}
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd
import (
"fmt"

"github.com/jemmyh/terminal_note/note"
"github.com/spf13/cobra"
"hujm.net/terminal_note/note"
)

// versionCmd represents the version command
Expand All @@ -35,6 +35,7 @@ func init() {
rootCmd.AddCommand(versionCmd)
}

// GetVersionStr returns the formated version string.
func GetVersionStr() string {
return fmt.Sprintf("%s @%s", note.GetVersion(), note.GetAppName())
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/jemmyh/terminal_note/cmd"

/*
* @CreateTime: 2021/1/14 15:31
* @Author: hujiaming
* @Author: JemmyHu <[email protected]>
* @Description:
*/

Expand Down
2 changes: 1 addition & 1 deletion note/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion note/note_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion note/note_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

/*
* @CreateTime: 2021/1/13 11:41
* @Author: hujiaming
* @Author: JemmyHu <[email protected]>
* @Description:
*/

Expand Down
2 changes: 1 addition & 1 deletion note/note_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

/*
* @CreateTime: 2021/1/12 20:49
* @Author: hujiaming
* @Author: JemmyHu <[email protected]>
* @Description: a iterator for note
*/

Expand Down
2 changes: 1 addition & 1 deletion note/note_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

/*
* @CreateTime: 2021/1/13 16:09
* @Author: hujiaming
* @Author: JemmyHu <[email protected]>
* @Description:
*/

Expand Down
18 changes: 1 addition & 17 deletions note/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*/

Expand All @@ -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())
}
37 changes: 16 additions & 21 deletions note/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 != "" {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion note/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

/*
* @CreateTime: 2021/1/13 19:46
* @Author: hujiaming
* @Author: JemmyHu <[email protected]>
* @Description:
*/

Expand Down
Loading

0 comments on commit b16d9bd

Please sign in to comment.