コード例 #1
0
ファイル: importer.go プロジェクト: sdgoij/TheDistributedBay
func WriteDbSignature(db core.Database, d chan *core.Torrent, totalRows int64) {
	start := time.Now()
	count := int64(0)
	open := true
	b := make([]*core.Torrent, 0)
	k, err := crypto.NewKey()
	if err != nil {
		panic(err)
	}
	for open {
		b = b[:0]
		for i := 0; i < 100 && open; i++ {
			var t *core.Torrent
			t, open = <-d
			if !open {
				log.Println("Finished import! Imported", count, "rows.")
				return
			}
			b = append(b, t)
		}
		count += int64(len(b))
		s, err := core.SignTorrents(k, b)
		if err != nil {
			panic(err)
		}
		db.AddSignature(s)
		if count%10000 == 0 {
			eta := time.Now().Sub(start) / time.Duration(count) * time.Duration(totalRows-count)
			log.Println("Signed: ", count, "of", totalRows, "(ETA:", eta.String()+")")
		}
	}
}
コード例 #2
0
func TestMerkle(t *testing.T) {
	k, err := crypto.NewKey()
	if err != nil {
		t.Fatal(err)
	}
	ts := make([]*Torrent, 0)

	for i := 0; i < 5; i++ {
		r := CreateTorrent([]byte(fmt.Sprint(i)), "n", "d", "cat", time.Now(), []string{"tag"}, 0, 0, 0, 0)
		ts = append(ts, r)
	}

	s, err := SignTorrents(k, ts)
	if err != nil {
		t.Fatal(err)
	}

	s2, err := SignTorrents(k, ts[:2])
	if err != nil {
		t.Fatal(err)
	}

	if s2.Hash() == s.Hash() {
		t.Fatalf("Hashes should be different %v v %v", s2.Hash(), s.Hash())
	}

	err = s.VerifySignature()
	if err != nil {
		t.Fatal(err)
	}

	for _, r := range ts {
		found := false
		for _, h := range s.ListTorrents() {
			if h == r.Hash {
				found = true
			}
		}
		if !found {
			t.Fatalf("Couldn't find torrent %s in %v", r.Hash, s.ListTorrents())
		}
	}

	// Make sure merkle verification fails.
	s.R.SetUint64(1)
	if s.VerifySignature() == nil {
		t.Fatal("Changing sig should cause failures")
	}

	s, err = SignTorrents(k, ts)
	if err != nil {
		t.Fatal(err)
	}

	s.Key.Curve = "badkey"
	if s.VerifySignature() == nil {
		t.Fatal("Changing key should cause failures")
	}

	s, err = SignTorrents(k, ts)
	if err != nil {
		t.Fatal(err)
	}

	s.M.l.r.hash = "badhash"
	if s.VerifySignature() == nil {
		t.Fatal("Changing key should cause failures")
	}

	s, err = SignTorrents(k, ts)
	if err != nil {
		t.Fatal(err)
	}

	s.M.r.l.hash = "badhash"
	if s.VerifySignature() == nil {
		t.Fatal("Changing key should cause failures")
	}
}