Allow convert_wycheproof.go to be used one file at a time.
Change-Id: I3c1d77ac9dea6faefc3711e84cf93191f35fe755
Reviewed-on: https://boringssl-review.googlesource.com/28705
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/util/convert_wycheproof.go b/util/convert_wycheproof.go
index f07621b..deabc75 100644
--- a/util/convert_wycheproof.go
+++ b/util/convert_wycheproof.go
@@ -132,7 +132,7 @@
}
}
-func convertWycheproof(jsonPath, txtPath string) error {
+func convertWycheproof(f io.Writer, jsonPath string) error {
jsonData, err := ioutil.ReadFile(jsonPath)
if err != nil {
return err
@@ -143,11 +143,6 @@
return err
}
- f, err := os.OpenFile(txtPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
- if err != nil {
- return err
- }
- defer f.Close()
if _, err := fmt.Fprintf(f, `# Imported from Wycheproof's %s.
# This file is generated by convert_wycheproof.go. Do not edit by hand.
#
@@ -225,34 +220,55 @@
return nil
}
+var defaultInputs = []string{
+ "aes_cbc_pkcs5_test.json",
+ "aes_gcm_siv_test.json",
+ "aes_gcm_test.json",
+ "chacha20_poly1305_test.json",
+ "dsa_test.json",
+ "ecdh_test.json",
+ "ecdsa_secp224r1_sha224_test.json",
+ "ecdsa_secp224r1_sha256_test.json",
+ "ecdsa_secp256r1_sha256_test.json",
+ "ecdsa_secp384r1_sha384_test.json",
+ "ecdsa_secp384r1_sha512_test.json",
+ "ecdsa_secp521r1_sha512_test.json",
+ "eddsa_test.json",
+ "kw_test.json",
+ "rsa_signature_test.json",
+ "x25519_test.json",
+}
+
func main() {
- jsonPaths := []string{
- "aes_cbc_pkcs5_test.json",
- "aes_cmac_test.json",
- "aes_gcm_siv_test.json",
- "aes_gcm_test.json",
- "chacha20_poly1305_test.json",
- "dsa_test.json",
- "ecdh_test.json",
- "ecdsa_secp224r1_sha224_test.json",
- "ecdsa_secp224r1_sha256_test.json",
- "ecdsa_secp256r1_sha256_test.json",
- "ecdsa_secp384r1_sha384_test.json",
- "ecdsa_secp384r1_sha512_test.json",
- "ecdsa_secp521r1_sha512_test.json",
- "eddsa_test.json",
- "kw_test.json",
- "rsa_signature_test.json",
- "x25519_test.json",
- }
- for _, jsonPath := range jsonPaths {
- if !strings.HasSuffix(jsonPath, ".json") {
- panic(jsonPath)
+ switch len(os.Args) {
+ case 1:
+ for _, jsonPath := range defaultInputs {
+ if !strings.HasSuffix(jsonPath, ".json") {
+ panic(jsonPath)
+ }
+
+ txtPath := jsonPath[:len(jsonPath)-len(".json")] + ".txt"
+ out, err := os.Create(txtPath)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error opening output %s: %s\n", txtPath, err)
+ os.Exit(1)
+ }
+ defer out.Close()
+
+ if err := convertWycheproof(out, jsonPath); err != nil {
+ fmt.Fprintf(os.Stderr, "Error converting %s: %s\n", jsonPath, err)
+ os.Exit(1)
+ }
}
- txtPath := jsonPath[:len(jsonPath)-len(".json")] + ".txt"
- if err := convertWycheproof(jsonPath, txtPath); err != nil {
- fmt.Fprintf(os.Stderr, "Error converting %s: %s\n", jsonPath, err)
+
+ case 2:
+ if err := convertWycheproof(os.Stdout, os.Args[1]); err != nil {
+ fmt.Fprintf(os.Stderr, "Error converting %s: %s\n", os.Args[1], err)
os.Exit(1)
}
+
+ default:
+ fmt.Fprintf(os.Stderr, "Usage: %s [input JSON]\n", os.Args[0])
+ os.Exit(1)
}
}