kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/tools/shasum_tool/shasum_tool.go (about)

     1  /*
     2   * Copyright 2017 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Binary shasum_tool calcuates the sha256 sum of the specified file.
    18  //
    19  // This is useful in environments which don't have analogous GNU tools
    20  // installed by default.
    21  // Example:
    22  //
    23  //	shasum_tool <input_file>
    24  package main
    25  
    26  import (
    27  	"crypto/sha256"
    28  	"encoding/hex"
    29  	"flag"
    30  	"fmt"
    31  	"io"
    32  	"os"
    33  	"path/filepath"
    34  
    35  	"kythe.io/kythe/go/util/log"
    36  )
    37  
    38  func main() {
    39  	flag.Parse()
    40  	if flag.NArg() != 1 {
    41  		log.Fatal("Usage: shasum_tool <input_file>")
    42  	}
    43  
    44  	inputFile := flag.Arg(0)
    45  	f, err := os.Open(inputFile)
    46  	if err != nil {
    47  		log.Fatal("Open: ", err)
    48  	}
    49  	defer f.Close()
    50  
    51  	hasher := sha256.New()
    52  	if _, err := io.Copy(hasher, f); err != nil {
    53  		log.Fatal("Hashing failed: ", err)
    54  	}
    55  	fmt.Printf("%s\t%s\n", hex.EncodeToString(hasher.Sum(nil)), filepath.Base(inputFile))
    56  }