Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
484d6004
提交
484d6004
authored
9月 27, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
crypto: abstracted Key and added Equals.
上级
c7bd8b78
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
75 行增加
和
18 行删除
+75
-18
key.go
crypto/key.go
+23
-6
key_test.go
crypto/key_test.go
+41
-1
rsa.go
crypto/rsa.go
+10
-0
handshake.go
crypto/spipe/handshake.go
+1
-11
没有找到文件。
crypto/key.go
浏览文件 @
484d6004
...
@@ -23,7 +23,17 @@ const (
...
@@ -23,7 +23,17 @@ const (
RSA
=
iota
RSA
=
iota
)
)
type
Key
interface
{
// Bytes returns a serialized, storeable representation of this key
Bytes
()
([]
byte
,
error
)
// Equals checks whether two PubKeys are the same
Equals
(
Key
)
bool
}
type
PrivKey
interface
{
type
PrivKey
interface
{
Key
// Cryptographically sign the given bytes
// Cryptographically sign the given bytes
Sign
([]
byte
)
([]
byte
,
error
)
Sign
([]
byte
)
([]
byte
,
error
)
...
@@ -32,17 +42,13 @@ type PrivKey interface {
...
@@ -32,17 +42,13 @@ type PrivKey interface {
// Generate a secret string of bytes
// Generate a secret string of bytes
GenSecret
()
[]
byte
GenSecret
()
[]
byte
// Bytes returns a serialized, storeable representation of this key
Bytes
()
([]
byte
,
error
)
}
}
type
PubKey
interface
{
type
PubKey
interface
{
Key
// Verify that 'sig' is the signed hash of 'data'
// Verify that 'sig' is the signed hash of 'data'
Verify
(
data
[]
byte
,
sig
[]
byte
)
(
bool
,
error
)
Verify
(
data
[]
byte
,
sig
[]
byte
)
(
bool
,
error
)
// Bytes returns a serialized, storeable representation of this key
Bytes
()
([]
byte
,
error
)
}
}
// Given a public key, generates the shared key.
// Given a public key, generates the shared key.
...
@@ -229,3 +235,14 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) {
...
@@ -229,3 +235,14 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) {
return
nil
,
ErrBadKeyType
return
nil
,
ErrBadKeyType
}
}
}
}
// KeyEqual checks whether two
func
KeyEqual
(
k1
,
k2
Key
)
bool
{
if
k1
==
k2
{
return
true
}
b1
,
err1
:=
k1
.
Bytes
()
b2
,
err2
:=
k2
.
Bytes
()
return
bytes
.
Equal
(
b1
,
b2
)
&&
err1
==
err2
}
crypto/key_test.go
浏览文件 @
484d6004
...
@@ -3,12 +3,14 @@ package crypto
...
@@ -3,12 +3,14 @@ package crypto
import
"testing"
import
"testing"
func
TestRsaKeys
(
t
*
testing
.
T
)
{
func
TestRsaKeys
(
t
*
testing
.
T
)
{
sk
,
_
,
err
:=
GenerateKeyPair
(
RSA
,
512
)
sk
,
pk
,
err
:=
GenerateKeyPair
(
RSA
,
512
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
testKeySignature
(
t
,
sk
)
testKeySignature
(
t
,
sk
)
testKeyEncoding
(
t
,
sk
)
testKeyEncoding
(
t
,
sk
)
testKeyEquals
(
t
,
sk
)
testKeyEquals
(
t
,
pk
)
}
}
func
testKeySignature
(
t
*
testing
.
T
,
sk
PrivKey
)
{
func
testKeySignature
(
t
*
testing
.
T
,
sk
PrivKey
)
{
...
@@ -52,3 +54,41 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
...
@@ -52,3 +54,41 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
}
}
func
testKeyEquals
(
t
*
testing
.
T
,
k
Key
)
{
kb
,
err
:=
k
.
Bytes
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
KeyEqual
(
k
,
k
)
{
t
.
Fatal
(
"Key not equal to itself."
)
}
if
!
KeyEqual
(
k
,
testkey
(
kb
))
{
t
.
Fatal
(
"Key not equal to key with same bytes."
)
}
sk
,
pk
,
err
:=
GenerateKeyPair
(
RSA
,
512
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
KeyEqual
(
k
,
sk
)
{
t
.
Fatal
(
"Keys should not equal."
)
}
if
KeyEqual
(
k
,
pk
)
{
t
.
Fatal
(
"Keys should not equal."
)
}
}
type
testkey
[]
byte
func
(
pk
testkey
)
Bytes
()
([]
byte
,
error
)
{
return
pk
,
nil
}
func
(
pk
testkey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
pk
,
k
)
}
crypto/rsa.go
浏览文件 @
484d6004
...
@@ -41,6 +41,11 @@ func (pk *RsaPublicKey) Bytes() ([]byte, error) {
...
@@ -41,6 +41,11 @@ func (pk *RsaPublicKey) Bytes() ([]byte, error) {
return
proto
.
Marshal
(
pbmes
)
return
proto
.
Marshal
(
pbmes
)
}
}
// Equals checks whether this key is equal to another
func
(
pk
*
RsaPublicKey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
pk
,
k
)
}
func
(
sk
*
RsaPrivateKey
)
GenSecret
()
[]
byte
{
func
(
sk
*
RsaPrivateKey
)
GenSecret
()
[]
byte
{
buf
:=
make
([]
byte
,
16
)
buf
:=
make
([]
byte
,
16
)
rand
.
Read
(
buf
)
rand
.
Read
(
buf
)
...
@@ -65,6 +70,11 @@ func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
...
@@ -65,6 +70,11 @@ func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
return
proto
.
Marshal
(
pbmes
)
return
proto
.
Marshal
(
pbmes
)
}
}
// Equals checks whether this key is equal to another
func
(
sk
*
RsaPrivateKey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
sk
,
k
)
}
func
UnmarshalRsaPrivateKey
(
b
[]
byte
)
(
*
RsaPrivateKey
,
error
)
{
func
UnmarshalRsaPrivateKey
(
b
[]
byte
)
(
*
RsaPrivateKey
,
error
)
{
sk
,
err
:=
x509
.
ParsePKCS1PrivateKey
(
b
)
sk
,
err
:=
x509
.
ParsePKCS1PrivateKey
(
b
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
crypto/spipe/handshake.go
浏览文件 @
484d6004
...
@@ -379,17 +379,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error)
...
@@ -379,17 +379,7 @@ func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error)
// did have pubkey, let's verify it's really the same.
// did have pubkey, let's verify it's really the same.
// this shouldn't ever happen, given we hashed, etc, but it could mean
// this shouldn't ever happen, given we hashed, etc, but it could mean
// expected code (or protocol) invariants violated.
// expected code (or protocol) invariants violated.
if
!
npeer
.
PubKey
.
Equals
(
rpk
)
{
lb
,
err1
:=
npeer
.
PubKey
.
Bytes
()
if
err1
!=
nil
{
return
nil
,
err1
}
rb
,
err2
:=
rpk
.
Bytes
()
if
err2
!=
nil
{
return
nil
,
err2
}
if
!
bytes
.
Equal
(
lb
,
rb
)
{
return
nil
,
fmt
.
Errorf
(
"WARNING: PubKey mismatch: %v"
,
npeer
.
ID
.
Pretty
())
return
nil
,
fmt
.
Errorf
(
"WARNING: PubKey mismatch: %v"
,
npeer
.
ID
.
Pretty
())
}
}
return
npeer
,
nil
return
npeer
,
nil
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论