Compare commits
1 Commits
a0df3ba8b0
...
e52426e80f
Author | SHA1 | Date | |
---|---|---|---|
e52426e80f |
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2020 Gregory Pomerantz
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
24
README.md
Normal file
24
README.md
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# b3sum
|
||||||
|
|
||||||
|
A golang port of b3sum
|
||||||
|
|
||||||
|
This is a simple `b3sum` command using `github.com/zeebo/blake3`.
|
||||||
|
|
||||||
|
```
|
||||||
|
go get git.wow.st/gmp/b3sum
|
||||||
|
go install git.wow.st/gmp/b3sum
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
b3sum [flags] [file]...
|
||||||
|
-h Prints help information
|
||||||
|
-l int
|
||||||
|
The number of output bytes, prior to hex encoding (default 32)
|
||||||
|
-p int
|
||||||
|
The maximum concurrency (defaults to the number of CPUs)
|
||||||
|
```
|
||||||
|
|
||||||
|
If no input files are specified, `b3sum` reads `stdin`.
|
||||||
|
|
37
main.go
37
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/zeebo/blake3"
|
"github.com/zeebo/blake3"
|
||||||
|
@ -14,27 +15,17 @@ import (
|
||||||
var (
|
var (
|
||||||
help = flag.Bool("h", false, "Prints help information")
|
help = flag.Bool("h", false, "Prints help information")
|
||||||
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding")
|
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding")
|
||||||
par = flag.Int("p", 4, "The maximum concurrency")
|
par = flag.Int("p", runtime.NumCPU(), "The maximum concurrency")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *help || len(flag.Args()) == 0 || *par < 1 {
|
if *help || *par < 1 {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
q := make(chan struct{}, *par)
|
sum := func(file io.ReadCloser) string {
|
||||||
wg := &sync.WaitGroup{}
|
|
||||||
|
|
||||||
for _, name := range flag.Args() {
|
|
||||||
q <- struct{}{}
|
|
||||||
wg.Add(1)
|
|
||||||
go func(name string) {
|
|
||||||
buf := [65536]byte{}
|
buf := [65536]byte{}
|
||||||
file, err := os.Open(name)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
hasher := blake3.NewSized(*length)
|
hasher := blake3.NewSized(*length)
|
||||||
for {
|
for {
|
||||||
n, err := file.Read(buf[:])
|
n, err := file.Read(buf[:])
|
||||||
|
@ -47,7 +38,24 @@ func main() {
|
||||||
}
|
}
|
||||||
file.Close()
|
file.Close()
|
||||||
hash := hasher.Sum([]byte{})
|
hash := hasher.Sum([]byte{})
|
||||||
fmt.Print(hex.EncodeToString(hash))
|
return hex.EncodeToString(hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(flag.Args()) == 0 {
|
||||||
|
fmt.Println(sum(os.Stdin))
|
||||||
|
} else {
|
||||||
|
q := make(chan struct{}, *par)
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
|
for _, name := range flag.Args() {
|
||||||
|
q <- struct{}{}
|
||||||
|
wg.Add(1)
|
||||||
|
go func(name string) {
|
||||||
|
file, err := os.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Print(sum(file))
|
||||||
fmt.Print(" ")
|
fmt.Print(" ")
|
||||||
fmt.Println(name)
|
fmt.Println(name)
|
||||||
<- q
|
<- q
|
||||||
|
@ -56,3 +64,4 @@ func main() {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user