github.com/bartle-stripe/trillian@v1.2.1/storage/tools/hasher/main.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // The hasher program provides a simple CLI for producing Merkle tree hashes. It is
    16  // intended for use in development or debugging storage code.
    17  package main
    18  
    19  import (
    20  	"encoding/base64"
    21  	"encoding/hex"
    22  	"flag"
    23  	"fmt"
    24  
    25  	"github.com/golang/glog"
    26  	"github.com/google/trillian"
    27  	"github.com/google/trillian/merkle/hashers"
    28  	_ "github.com/google/trillian/merkle/rfc6962" // Load hashers
    29  )
    30  
    31  var (
    32  	hashStrategyFlag = flag.String("hash_strategy", "RFC6962_SHA256", "The log hashing strategy to use")
    33  	base64Flag       = flag.Bool("base64", false, "If true output in base64 instead of hex")
    34  )
    35  
    36  func createHasher() hashers.LogHasher {
    37  	strategy, ok := trillian.HashStrategy_value[*hashStrategyFlag]
    38  	if !ok {
    39  		glog.Fatalf("Unknown hash strategy: %s", *hashStrategyFlag)
    40  	}
    41  
    42  	hasher, err := hashers.NewLogHasher(trillian.HashStrategy(strategy))
    43  	if err != nil {
    44  		glog.Fatalf("Failed to create a log hasher for strategy %s: %v", *hashStrategyFlag, err)
    45  	}
    46  
    47  	return hasher
    48  }
    49  
    50  func decodeArgs(args []string) [][]byte {
    51  	dec := make([][]byte, 0, len(args))
    52  
    53  	for _, arg := range args {
    54  		dh, err := hex.DecodeString(arg)
    55  		if err != nil {
    56  			glog.Fatalf("Input arg not a hex encoded string: %s: %v", arg, err)
    57  		}
    58  		dec = append(dec, dh)
    59  	}
    60  
    61  	return dec
    62  }
    63  
    64  func main() {
    65  	flag.Parse()
    66  	defer glog.Flush()
    67  
    68  	hasher := createHasher()
    69  	decoded := decodeArgs(flag.Args())
    70  	var hash []byte
    71  
    72  	switch len(decoded) {
    73  	case 1:
    74  		// Leaf hash requested
    75  		hashLeaf, err := hasher.HashLeaf(decoded[0])
    76  		if err != nil {
    77  			glog.Exitf("HashLeaf(%v): %v", decoded[0], err)
    78  		}
    79  		hash = hashLeaf
    80  
    81  	case 2:
    82  		// Node hash requested
    83  		hash = hasher.HashChildren(decoded[0], decoded[1])
    84  
    85  	default:
    86  		glog.Fatalf("Invalid number of arguments expected 1 (for leaf) or 2 (for node)")
    87  	}
    88  
    89  	if *base64Flag {
    90  		fmt.Printf("%s\n", base64.StdEncoding.EncodeToString(hash))
    91  	} else {
    92  		fmt.Printf("%s\n", hex.EncodeToString(hash))
    93  	}
    94  }