-
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.
- Loading branch information
1 parent
6e6936a
commit a103eeb
Showing
13 changed files
with
269 additions
and
18 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
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,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"bandr.me/p/pocryp/internal/cli/cmd" | ||
"bandr.me/p/pocryp/internal/she/mup" | ||
) | ||
|
||
var DecodeCmd = &cmd.Command{ | ||
Name: "she-decode", | ||
Run: runDecode, | ||
Brief: "Decode M1,M2,M3,M4,M5 to JSON input", | ||
|
||
Usage: `Usage: pocryp she-decode [hex_string] | ||
Decode the M1,M2,M3,M4,M5 given as a hex string to its JSON form. | ||
If no argument given, stdin will be read. | ||
`, | ||
} | ||
|
||
func runDecode(cmd *cmd.Command) error { | ||
keyHex := cmd.Flags.String("key", "", "Secret key as hex") | ||
|
||
if err := cmd.Parse(); err != nil { | ||
return err | ||
} | ||
|
||
input := cmd.Flags.Arg(0) | ||
if cmd.Flags.NArg() == 0 { | ||
data, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return fmt.Errorf("failed to read stdin: %w", err) | ||
} | ||
input = string(data) | ||
} | ||
input = strings.NewReplacer(" ", "", "\t", "", "\r", "", "\n", "").Replace(input) | ||
|
||
key, err := hex.DecodeString(*keyHex) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode key: %w", err) | ||
} | ||
if len(key) != 16 { | ||
return fmt.Errorf("invalid key size: %d", len(key)) | ||
} | ||
|
||
m1m2m3, err := hex.DecodeString(input) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode input: %w", err) | ||
} | ||
|
||
result, err := mup.Decode(m1m2m3, key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
enc := json.NewEncoder(os.Stdout) | ||
enc.SetIndent("", " ") | ||
|
||
return enc.Encode(result) | ||
} |
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,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"bandr.me/p/pocryp/internal/cli/cmd" | ||
"bandr.me/p/pocryp/internal/she/mup" | ||
) | ||
|
||
var EncodeCmd = &cmd.Command{ | ||
Name: "she-encode", | ||
Run: runEncode, | ||
Brief: "Encode JSON input to M1,M2,M3,M4,M5", | ||
|
||
Usage: `Usage: pocryp she-encode [input.json] | ||
Encode the given JSON input file to M1,M2,M3,M4,M5. | ||
If no argument given, stdin will be read. | ||
`, | ||
} | ||
|
||
func runEncode(cmd *cmd.Command) error { | ||
oneLine := cmd.Flags.Bool("l", false, "Print everything on one line") | ||
|
||
if err := cmd.Parse(); err != nil { | ||
return err | ||
} | ||
|
||
inFile := os.Stdin | ||
if cmd.Flags.NArg() > 0 { | ||
f, err := os.Open(cmd.Flags.Arg(0)) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
inFile = f | ||
} | ||
|
||
var input mup.Input | ||
if err := json.NewDecoder(inFile).Decode(&input); err != nil { | ||
return err | ||
} | ||
|
||
result, err := input.Encode() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *oneLine { | ||
fmt.Println(hex.EncodeToString(result[:])) | ||
} else { | ||
m1, m2, m3, m4, m5 := mup.SliceMs(result) | ||
fmt.Println(hex.EncodeToString(m1)) | ||
fmt.Println(hex.EncodeToString(m2)) | ||
fmt.Println(hex.EncodeToString(m3)) | ||
fmt.Println(hex.EncodeToString(m4)) | ||
fmt.Println(hex.EncodeToString(m5)) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestEncode(t *testing.T) { | ||
EncodeCmd.Args = []string{"-foo"} | ||
EncodeCmd.Init() | ||
if err := runEncode(EncodeCmd); err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} |
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,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"bandr.me/p/pocryp/internal/cli/cmd" | ||
"bandr.me/p/pocryp/internal/she/mup" | ||
) | ||
|
||
var ExampleCmd = &cmd.Command{ | ||
Name: "she-example", | ||
Run: runExample, | ||
Brief: "Print example JSON input", | ||
|
||
Usage: `Usage: pocryp she-example | ||
Print example JSON input to stdout. | ||
`, | ||
} | ||
|
||
func runExample(cmd *cmd.Command) error { | ||
if err := cmd.Parse(); err != nil { | ||
return err | ||
} | ||
|
||
enc := json.NewEncoder(os.Stdout) | ||
enc.SetIndent("", " ") | ||
|
||
input := mup.Input{ | ||
UID: "000000000000000000000000000000", | ||
ID: 0, | ||
AuthID: 0, | ||
AuthKey: "00000000000000000000000000000000", | ||
NewKey: "00000000000000000000000000000000", | ||
} | ||
|
||
return enc.Encode(input) | ||
} |
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,15 @@ | ||
{ | ||
"UID": "000000000000000000000000000001", | ||
"AuthID": 1, | ||
"AuthKey": "000102030405060708090a0b0c0d0e0f", | ||
"ID": 4, | ||
"NewKey": "0f0e0d0c0b0a09080706050403020100", | ||
"Counter": 1, | ||
"Flags": { | ||
"Write": false, | ||
"Boot": false, | ||
"Debugger": false, | ||
"KeyUsage": false, | ||
"Wildcard": false | ||
} | ||
} |
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,13 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestExample(t *testing.T) { | ||
ExampleCmd.Args = []string{"-foo"} | ||
ExampleCmd.Init() | ||
if err := runExample(ExampleCmd); err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} |
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