Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1369d91375 | |||
| bf5d18e73a |
@ -21,6 +21,7 @@ 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,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -15,6 +17,7 @@ func help() {
|
|||||||
fmt.Println("\thashcash new [subject *] [difficulty 10] [expires in 5m]")
|
fmt.Println("\thashcash new [subject *] [difficulty 10] [expires in 5m]")
|
||||||
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 *]")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +110,17 @@ 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 nargs < 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 {
|
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
|
||||||
@ -227,15 +231,12 @@ 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 false
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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