Skip to content

Instantly share code, notes, and snippets.

@jzelinskie
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzelinskie/11fa2638b01388295241 to your computer and use it in GitHub Desktop.
Save jzelinskie/11fa2638b01388295241 to your computer and use it in GitHub Desktop.
print an infohash
package main
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"net/url"
"github.com/chihaya/bencode"
)
func main() {
file, err := ioutil.ReadFile("/Users/jimi/example.json.torrent")
if err != nil {
log.Fatal(err)
}
i, err := bencode.Unmarshal(file)
if err != nil {
log.Fatal(err)
}
fileDict, ok := i.(bencode.Dict)
if !ok {
log.Fatal(err)
}
sha := sha1.New()
e := bencode.NewEncoder(sha)
err = e.Encode(fileDict["info"])
if err != nil {
log.Fatal(err)
}
fmt.Printf("Infohash: %x\n", sha.Sum(nil))
fmt.Println("EscapedInfohash: ", url.QueryEscape(string(sha.Sum(nil))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment