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