Skip to content

Commit

Permalink
implement sign
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Aug 14, 2024
1 parent 66cb40f commit 720a128
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/hppktool/cmd/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 xtaci <imap@live.com>
*/
package cmd

Expand All @@ -20,7 +20,7 @@ import (
var encryptCmd = &cobra.Command{
Use: "encrypt",
Short: "encrypts a message from standard input",
Long: `the message will first be SHA256 hashed and then encrypted using AES256`,
Long: `the message will first be SHA256 hashed and then encrypted using AES256 unless -raw is specified`,
Run: func(cmd *cobra.Command, args []string) {
silent, err := cmd.Flags().GetBool("silent")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hppktool/cmd/keygen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 xtaci <imap@live.com>
*/
package cmd

Expand Down
3 changes: 1 addition & 2 deletions cmd/hppktool/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 xtaci <imap@live.com>
*/
package cmd

Expand Down Expand Up @@ -37,6 +37,5 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().BoolP("silent", "s", false, "Suppress non-vital message")
}
101 changes: 91 additions & 10 deletions cmd/hppktool/cmd/sign.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,106 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 xtaci <imap@live.com>
*/
package cmd

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"github.com/xtaci/hppk"
)

// signCmd represents the sign command
var signCmd = &cobra.Command{
Use: "sign",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "sign a message from standard input",
Long: `the message will first be SHA256 hashed and then encrypted using AES256 unless -raw is specified`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("sign called")
silent, err := cmd.Flags().GetBool("silent")
if err != nil {
panic("cann't get param -> silent")
}

paramIdentity, err := cmd.Flags().GetString("identity")
if err != nil {
panic("cannot get param -> pubkey")
}

paramRaw, err := cmd.Flags().GetBool("raw")
if err != nil {
panic("cannot get param -> raw")
}

// open the public key file
fIdentity, err := os.Open(paramIdentity)
if err != nil {
fmt.Println(err)
return
}
defer fIdentity.Close()

// sign the hash with the public key
priv := &hppk.PrivateKey{}
err = json.NewDecoder(fIdentity).Decode(priv)
if err != nil {
fmt.Println(err)
return
}

// read from standard input and hash it
var message []byte
if paramRaw {
message = make([]byte, 256)
count := 0
lr := io.LimitReader(os.Stdin, 256)
for {
n, err := lr.Read(message[count:])
count += n
if err == io.EOF {
break
}

if err != nil {
fmt.Println(err)
return
}
}
message = message[:count]
if !silent {
fmt.Printf("RAW(hex):%v\n", hex.EncodeToString(message))
}
} else {
h := sha256.New()
if _, err := io.Copy(h, os.Stdin); err != nil {
fmt.Println(err)
return
}
message = h.Sum(nil)
if !silent {
fmt.Printf("SHA256(hex):%v\n", hex.EncodeToString(message))
}
}

// encrypt the message
sig, err := priv.Sign(message)
var jsonBuffer bytes.Buffer
err = json.NewEncoder(&jsonBuffer).Encode(sig)
if err != nil {
fmt.Println(err)
return
}

if !silent {
fmt.Printf("Signature:\n")
}
fmt.Print(string(jsonBuffer.Bytes()))

},
}

Expand All @@ -37,4 +116,6 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// signCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
signCmd.Flags().StringP("identity", "p", "./id_hppk", "the hppk private key file")
signCmd.Flags().Bool("raw", false, "encrypt the raw message, the message length must not exceed 256 bytes")
}
3 changes: 1 addition & 2 deletions cmd/hppktool/cmd/verify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 xtaci <imap@live.com>
*/
package cmd

Expand Down

0 comments on commit 720a128

Please sign in to comment.