diff options
author | RishabhBhatnagar <bhatnagarrishabh4@gmail.com> | 2020-01-09 20:39:55 +0530 |
---|---|---|
committer | RishabhBhatnagar <bhatnagarrishabh4@gmail.com> | 2020-01-09 21:04:37 +0530 |
commit | cd59ee66408a908f7ef94548814514f6bc9fc906 (patch) | |
tree | 550b146d4de0cc00a4784147f7d8f2a7bc93cffe /utils/verification.go | |
parent | f4fef41a45620391fca6481f4700b89de170ab88 (diff) | |
download | spdx-tools-cd59ee66408a908f7ef94548814514f6bc9fc906.tar.gz |
Create Go Module
- Unpack directory v0 to move all the content to the root directory.
- ./v0/* converted to ./*
- all the test cases were fixed to remove one directory less indexing for test files
- add go.mod
- go version 1.13 is used to have a relatively stable versioning system
Signed-off-by: RishabhBhatnagar <bhatnagarrishabh4@gmail.com>
Diffstat (limited to 'utils/verification.go')
-rw-r--r-- | utils/verification.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/utils/verification.go b/utils/verification.go new file mode 100644 index 0000000..5fbf62b --- /dev/null +++ b/utils/verification.go @@ -0,0 +1,43 @@ +// Package utils contains various utility functions to support the +// main tools-golang packages. +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +package utils + +import ( + "crypto/sha1" + "fmt" + "sort" + "strings" + + "github.com/spdx/tools-golang/spdx" +) + +// GetVerificationCode2_1 takes a slice of files and an optional filename +// for an "excludes" file, and returns a Package Verification Code calculated +// according to SPDX spec version 2.1, section 3.9.4. +func GetVerificationCode2_1(files []*spdx.File2_1, excludeFile string) (string, error) { + // create slice of strings - unsorted SHA1s for all files + shas := []string{} + for i, f := range files { + if f == nil { + return "", fmt.Errorf("got nil file in index %d", i) + } + if f.FileName != excludeFile { + shas = append(shas, f.FileChecksumSHA1) + } + } + + // sort the strings + sort.Strings(shas) + + // concatenate them into one string, with no trailing separators + shasConcat := strings.Join(shas, "") + + // and get its SHA1 value + hsha1 := sha1.New() + hsha1.Write([]byte(shasConcat)) + bs := hsha1.Sum(nil) + code := fmt.Sprintf("%x", bs) + + return code, nil +} |