-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
) | ||
|
||
// 基于二叉树实现 哈希树 | ||
// Git 用于比对文件系统差异时提升效率 | ||
// P2P下载中BT种子的构成是hash列表(按块拆分文件后做hash得到) | ||
// https://en.wikipedia.org/wiki/Merkle_tree | ||
// https://yeasy.gitbook.io/blockchain_guide/05_crypto/merkle_trie | ||
|
||
type ( | ||
Merkle struct { | ||
tree *BinTree[string] | ||
} | ||
) | ||
|
||
func CreateMerkleByFile(path string) *Merkle { | ||
// 对文件分块计算Hash构造树并存储 | ||
return nil | ||
} | ||
|
||
func CreateMerkle(hash []string) *Merkle { | ||
if len(hash) == 0 { | ||
return nil | ||
} | ||
|
||
var tmp []*BinTree[string] | ||
for _, s := range hash { | ||
tmp = append(tmp, &BinTree[string]{Data: s}) | ||
} | ||
for { | ||
if len(tmp) == 1 { | ||
break | ||
} | ||
var par []*BinTree[string] | ||
var node *BinTree[string] | ||
for i := range tmp { | ||
if node == nil { | ||
node = &BinTree[string]{Left: tmp[i]} | ||
par = append(par, node) | ||
} else { | ||
node.Right = tmp[i] | ||
node = nil | ||
} | ||
} | ||
for _, b := range par { | ||
fillData(b) | ||
} | ||
tmp = par | ||
} | ||
return &Merkle{tree: tmp[0]} | ||
} | ||
func (m *Merkle) Same(merkle *Merkle) bool { | ||
if merkle == nil { | ||
return false | ||
} | ||
return m.tree.Data == merkle.tree.Data | ||
} | ||
func fillData(node *BinTree[string]) { | ||
if node == nil { | ||
return | ||
} | ||
c := node.Left.Data | ||
if node.Right != nil { | ||
c += " " + node.Right.Data | ||
} | ||
|
||
node.Data = hash(c) | ||
} | ||
func hash(val string) string { | ||
sum := md5.Sum([]byte(val)) | ||
return fmt.Sprintf("%x", sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kuangcp/gobase/pkg/ctool/algo" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestInitMerkleByHash(t *testing.T) { | ||
merkle := CreateMerkle([]string{"a", "b", "c", "d", "e", "f"}) | ||
|
||
println(algo.PrintBiMindMap(merkle.tree)) | ||
} | ||
|
||
func TestMerkle_Same(t *testing.T) { | ||
a := CreateMerkle([]string{"a", "b", "c", "d", "e", "f"}) | ||
println(algo.PrintBiMindMap(a.tree)) | ||
|
||
b := CreateMerkle([]string{"a", "b", "c", "d", "e", "f"}) | ||
println(algo.PrintBiMindMap(b.tree)) | ||
|
||
c := CreateMerkle([]string{"a", "b", "c", "d", "e"}) | ||
println(algo.PrintBiMindMap(c.tree)) | ||
|
||
assert.Equal(t, a.Same(b), true) | ||
assert.Equal(t, a.Same(c), false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go 1.19 | ||
// | ||
//use ( | ||
// . | ||
// huffman | ||
// linkedlist | ||
//) | ||
// | ||
//replace ( | ||
// github.com/kuangcp/gobase/pkg/ctool v1.1.5 => ../pkg/ctool | ||
//) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module system | ||
|
||
// 系统级信息 | ||
|
||
go 1.20 | ||
|
||
require ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters