Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1369d91375 | |||
| bf5d18e73a | |||
| d2076da9a0 | |||
| 6af1527e7a |
@ -4,7 +4,7 @@ HTTP Hashcash implemented in Go.
|
||||
|
||||
Explanation at https://therootcompany.com/blog/http-hashcash/
|
||||
|
||||
Go docs at https://godoc.org/git.rootprojects.org/root/hashcash
|
||||
Go API docs at https://pkg.go.dev/git.rootprojects.org/root/hashcash?tab=doc
|
||||
|
||||
# CLI Usage
|
||||
|
||||
@ -21,6 +21,7 @@ Usage:
|
||||
hashcash new [subject *] [expires in 5m] [difficulty 10]
|
||||
hashcash parse <hashcash>
|
||||
hashcash solve <hashcash>
|
||||
hashcash hash <hashcash>
|
||||
hashcash verify <hashcash> [subject *]
|
||||
```
|
||||
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -12,16 +14,18 @@ import (
|
||||
|
||||
func help() {
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println("\thashcash new [subject *] [expires in 5m] [difficulty 10]")
|
||||
fmt.Println("\thashcash new [subject *] [difficulty 10] [expires in 5m]")
|
||||
fmt.Println("\thashcash parse <hashcash>")
|
||||
fmt.Println("\thashcash solve <hashcash>")
|
||||
fmt.Println("\thashcash hash <hashcash>")
|
||||
fmt.Println("\thashcash verify <hashcash> [subject *]")
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[:]
|
||||
nargs := len(args)
|
||||
|
||||
if len(args) < 2 {
|
||||
if nargs < 2 {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
@ -30,27 +34,30 @@ func main() {
|
||||
switch args[1] {
|
||||
case "new":
|
||||
var subject string
|
||||
if len(args) > 3 {
|
||||
if nargs >= 3 {
|
||||
subject = args[2]
|
||||
}
|
||||
|
||||
var difficulty int
|
||||
if len(args) > 4 {
|
||||
if nargs >= 4 {
|
||||
var err error
|
||||
difficulty, err = strconv.Atoi(args[3])
|
||||
if nil != err {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var expIn time.Duration
|
||||
if len(args) > 5 {
|
||||
if nargs >= 5 {
|
||||
var err error
|
||||
expIn, err = time.ParseDuration(args[4])
|
||||
if nil != err {
|
||||
fmt.Println(err)
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
expIn = 5 * time.Minute
|
||||
@ -66,7 +73,7 @@ func main() {
|
||||
return
|
||||
case "parse":
|
||||
var token string
|
||||
if 3 != len(args) {
|
||||
if 3 != nargs {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
@ -82,7 +89,7 @@ func main() {
|
||||
return
|
||||
case "solve":
|
||||
var token string
|
||||
if 3 != len(args) {
|
||||
if 3 != nargs {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
@ -103,9 +110,20 @@ func main() {
|
||||
|
||||
fmt.Println(h.String())
|
||||
return
|
||||
case "hash":
|
||||
var token string
|
||||
if 3 != nargs {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
token = args[2]
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
fmt.Println(hex.EncodeToString(hash[:]))
|
||||
return
|
||||
case "verify":
|
||||
var token string
|
||||
if len(args) < 3 {
|
||||
if nargs < 3 {
|
||||
help()
|
||||
os.Exit(1)
|
||||
return
|
||||
@ -118,7 +136,7 @@ func main() {
|
||||
}
|
||||
|
||||
subject := "*"
|
||||
if len(args) > 3 {
|
||||
if nargs >= 4 {
|
||||
subject = args[3]
|
||||
}
|
||||
|
||||
|
||||
17
hashcash.go
17
hashcash.go
@ -216,6 +216,10 @@ func (h *Hashcash) Verify(subject string) error {
|
||||
}
|
||||
|
||||
func verifyBits(hash []byte, bits, n int) bool {
|
||||
if 0 == bits {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if bits > 8 {
|
||||
bits -= 8
|
||||
@ -227,15 +231,12 @@ func verifyBits(hash []byte, bits, n int) bool {
|
||||
|
||||
// (bits % 8) == bits
|
||||
pad := 8 - bits
|
||||
if 0 != hash[i]>>pad {
|
||||
if 0 == hash[i]>>pad {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 0 == bits
|
||||
return true
|
||||
}
|
||||
|
||||
// Solve will search for a solution, returning an error if the difficulty is
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user