-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d1f167
commit 00b8acd
Showing
29 changed files
with
402 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
|
||
"github.com/candiddev/shared/go/cli" | ||
"github.com/candiddev/shared/go/errs" | ||
"github.com/candiddev/shared/go/logger" | ||
) | ||
|
||
func cmdBase64() cli.Command[*cfg] { | ||
return cli.Command[*cfg]{ | ||
ArgumentsRequired: []string{ | ||
"input value or - for stdin", | ||
}, | ||
Flags: cli.Flags{ | ||
"d": { | ||
Usage: "Decode base64 (default: encode)", | ||
}, | ||
"r": { | ||
Usage: "Raw/no padding (default: padding)", | ||
}, | ||
"u": { | ||
Usage: "URL encoding (default: standard encoding)", | ||
}, | ||
}, | ||
Usage: "Encode/decode a base64 value or stdin and output to stdout.", | ||
Run: func(ctx context.Context, args []string, f cli.Flags, c *cfg) errs.Err { | ||
v := []byte(args[1]) | ||
if string(v) == "-" { | ||
v = cli.ReadStdin() | ||
} | ||
|
||
_, d := f.Value("d") | ||
_, url := f.Value("u") | ||
_, r := f.Value("r") | ||
|
||
var err error | ||
|
||
var out []byte | ||
|
||
switch { | ||
case d && url && r: | ||
out, err = base64.RawURLEncoding.DecodeString(string(v)) | ||
case d && r: | ||
out, err = base64.RawStdEncoding.DecodeString(string(v)) | ||
case d && url: | ||
out, err = base64.URLEncoding.DecodeString(string(v)) | ||
case d: | ||
out, err = base64.StdEncoding.DecodeString(string(v)) | ||
case r && url: | ||
out = []byte(base64.RawURLEncoding.EncodeToString(v)) | ||
case r: | ||
out = []byte(base64.RawStdEncoding.EncodeToString(v)) | ||
case url: | ||
out = []byte(base64.URLEncoding.EncodeToString(v)) | ||
default: | ||
out = []byte(base64.StdEncoding.EncodeToString(v)) | ||
} | ||
|
||
if err != nil { | ||
return logger.Error(ctx, errs.ErrReceiver.Wrap(err)) | ||
} | ||
|
||
logger.Stdout.Write(out) //nolint:errcheck | ||
|
||
return nil | ||
}, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
|
||
"github.com/candiddev/shared/go/cli" | ||
"github.com/candiddev/shared/go/cryptolib" | ||
"github.com/candiddev/shared/go/errs" | ||
"github.com/candiddev/shared/go/logger" | ||
) | ||
|
||
func cmdGenSig() cli.Command[*cfg] { | ||
return cli.Command[*cfg]{ | ||
ArgumentsRequired: []string{ | ||
"private key value or encrypted value name", | ||
"data to sign or - for stdin", | ||
}, | ||
Flags: cli.Flags{ | ||
"s": { | ||
Usage: "Output just the signature", | ||
}, | ||
}, | ||
Usage: "Generate a signature and output a standard encoding base64 string. Must specify the private key of the signer and the data to be signed. For ECP keys, the hash will be SHA256. For Ed25519, the hash is unused.", | ||
Run: func(ctx context.Context, args []string, f cli.Flags, c *cfg) errs.Err { | ||
data := []byte(args[2]) | ||
if string(data) == "-" { | ||
data = cli.ReadStdin() | ||
} | ||
|
||
privateKey, errr := c.decryptValuePrivateKey(ctx, args[1]) | ||
if errr != nil { | ||
return logger.Error(ctx, errr) | ||
} | ||
|
||
s, err := cryptolib.NewSignature(privateKey, data) | ||
if err != nil { | ||
return logger.Error(ctx, errs.ErrReceiver.Wrap(err)) | ||
} | ||
|
||
if _, sig := f.Value("s"); sig { | ||
logger.Raw(base64.StdEncoding.EncodeToString(s.Signature)) | ||
} else { | ||
logger.Raw(s.String() + "\n") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/candiddev/shared/go/cli" | ||
"github.com/candiddev/shared/go/cryptolib" | ||
"github.com/candiddev/shared/go/errs" | ||
"github.com/candiddev/shared/go/logger" | ||
) | ||
|
||
func cmdVerifySig() cli.Command[*cfg] { | ||
return cli.Command[*cfg]{ | ||
ArgumentsRequired: []string{ | ||
"public key value, encrypted value name, or path", | ||
"message or - for stdin", | ||
"signature", | ||
}, | ||
Usage: "Verify a signature for a message using a public key. Signature must be in the form <hash>:<signature>:<optional key id>.", | ||
Run: func(ctx context.Context, args []string, f cli.Flags, c *cfg) errs.Err { | ||
pk, err := c.publicKey(args[1]) | ||
if err != nil { | ||
return logger.Error(ctx, errs.ErrReceiver.Wrap(err)) | ||
} | ||
|
||
m := []byte(args[2]) | ||
if string(m) == "-" { | ||
m = cli.ReadStdin() | ||
} | ||
|
||
s, err := cryptolib.ParseSignature(args[3]) | ||
if err != nil { | ||
return logger.Error(ctx, errs.ErrReceiver.Wrap(err)) | ||
} | ||
|
||
if err := s.Verify(m, cryptolib.Keys[cryptolib.KeyProviderPublic]{ | ||
pk, | ||
}); err != nil { | ||
return logger.Error(ctx, errs.ErrReceiver.Wrap(err)) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.