Skip to content

Commit

Permalink
Added QR code generation feature for easier share distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
49pctber committed Sep 18, 2024
1 parent c952e12 commit 75a0416
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 65 deletions.
142 changes: 78 additions & 64 deletions cmd/shamir/cmd/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

shamir "github.com/49pctber/shamir"
"github.com/spf13/cobra"

"github.com/skip2/go-qrcode"
)

var distributeCmd = &cobra.Command{
Expand All @@ -16,56 +18,89 @@ var distributeCmd = &cobra.Command{
Long: `Distrbute a secret S into n shares, where any k shares can reconstruct S.`,
}

var distributeFileCmd = &cobra.Command{
Use: "file [filename]",
Short: "distributes a file",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
invalid_command := false
func parseInput(cmd *cobra.Command) (int, int, int, bool) {
invalid_command := false

nshares, err := cmd.Flags().GetInt("nshares")
if err != nil || nshares < 2 {
fmt.Println("provide n >= 2")
invalid_command = true
}
nshares, err := cmd.Flags().GetInt("nshares")
if err != nil || nshares < 2 {
fmt.Println("provide n >= 2")
invalid_command = true
}

threshold, err := cmd.Flags().GetInt("threshold")
if err != nil || threshold < 2 {
fmt.Println("provide k >= 2")
invalid_command = true
}
threshold, err := cmd.Flags().GetInt("threshold")
if err != nil || threshold < 2 {
fmt.Println("provide k >= 2")
invalid_command = true
}

primitivePoly, err := cmd.Flags().GetInt("primitive")
if err != nil {
fmt.Println("error reading primitive polynomial")
invalid_command = true
}
primitivePoly, err := cmd.Flags().GetInt("primitive")
if err != nil {
fmt.Println("error reading primitive polynomial")
invalid_command = true
}

if invalid_command {
os.Exit(1)
qr, _ := cmd.Flags().GetBool("qr")

if invalid_command {
os.Exit(1)
}

return nshares, threshold, primitivePoly, qr
}

func distributeQRCodes(s *shamir.Shamir) {
for _, share := range s.GetShares() {
fname, err := filepath.Abs(share.ShareLabel() + ".png")
if err != nil {
fmt.Printf("error saving QR code: %v\n", err)
break
}

secretstring, err := os.ReadFile(args[0])
err = qrcode.WriteFile(share.String(), qrcode.High, -5, fname)
if err != nil {
fmt.Println("error reading file")
os.Exit(1)
fmt.Printf("error saving QR code: %v\n", err)
break
}

fmt.Printf("Generating %d-of-%d secret sharing scheme...\n", threshold, nshares)
fmt.Printf("saved QR code to %s\n", fname)
}
}

func generateSecret(secret []byte, primitivePoly, nshares, threshold int) *shamir.Shamir {
fmt.Printf("Generating %d-of-%d secret sharing scheme...\n", threshold, nshares)

s, err := shamir.NewShamirSecret(primitivePoly, nshares, threshold, []byte(secretstring))
s, err := shamir.NewShamirSecret(primitivePoly, nshares, threshold, secret)
if err != nil {
fmt.Printf("error distributing secret: %v\n", err)
os.Exit(1)
}

return s
}

var distributeFileCmd = &cobra.Command{
Use: "file [filename]",
Short: "distributes a file",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

nshares, threshold, primitivePoly, qr := parseInput(cmd)

dir, err := os.Getwd()
if err != nil {
fmt.Printf("error distributing secret: %v\n", err)
fmt.Printf("error getting current directory: %v\n", dir)
os.Exit(1)
}

dir, err := os.Getwd()
secret, err := os.ReadFile(args[0])
if err != nil {
fmt.Printf("error getting current directory: %v\n", dir)
fmt.Println("error reading file")
os.Exit(1)
}

s := generateSecret(secret, primitivePoly, nshares, threshold)

for _, share := range s.GetShares() {

fname := filepath.Clean(path.Join(dir, fmt.Sprintf("%s.txt", share.ShareLabel())))
Expand All @@ -77,6 +112,10 @@ var distributeFileCmd = &cobra.Command{

fmt.Printf("%s saved to %s\n", share.ShareLabel(), fname)
}

if qr {
distributeQRCodes(s)
}
},
}

Expand All @@ -87,42 +126,16 @@ var distributeStringCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

invalid_command := false

nshares, err := cmd.Flags().GetInt("nshares")
if err != nil || nshares < 2 {
fmt.Println("provide n >= 2")
invalid_command = true
}

threshold, err := cmd.Flags().GetInt("threshold")
if err != nil || threshold < 2 {
fmt.Println("provide k >= 2")
invalid_command = true
}

primitivePoly, err := cmd.Flags().GetInt("primitive")
if err != nil {
fmt.Println("error reading primitive polynomial")
invalid_command = true
}

if invalid_command {
os.Exit(1)
}

secretstring := args[0]
nshares, threshold, primitivePoly, qr := parseInput(cmd)
s := generateSecret([]byte(secretstring), primitivePoly, nshares, threshold)

fmt.Printf("Generating %d-of-%d secret sharing scheme...\n", threshold, nshares)

s, err := shamir.NewShamirSecret(primitivePoly, nshares, threshold, []byte(secretstring))
if err != nil {
fmt.Printf("error distributing secret: %v\n", err)
os.Exit(1)
for _, share := range s.GetShares() {
fmt.Printf("%s\n", share.String())
}

for i := range nshares {
fmt.Printf("%s\n", s.ShareString(i))
if qr {
distributeQRCodes(s)
}
},
}
Expand All @@ -132,6 +145,7 @@ func init() {
distributeCmd.PersistentFlags().IntP("nshares", "n", 0, "number of shares to produce")
distributeCmd.PersistentFlags().IntP("threshold", "k", 0, "the number of shares needed to reconstruct the secret")
distributeCmd.PersistentFlags().IntP("primitive", "p", 0x11d, "primitive polynomial to use when constructing Galois field")
distributeCmd.PersistentFlags().Bool("qr", false, "indicates QR codes of shares should be generated and saved in current directory")

distributeCmd.AddCommand(distributeFileCmd)

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/49pctber/shamir

go 1.22.3

require github.com/spf13/cobra v1.8.1
require (
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.8.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down

0 comments on commit 75a0416

Please sign in to comment.