Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
7b501906
Commit
7b501906
authored
Apr 13, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: separate out magic number from the code
parent
5467e7b3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
25 deletions
+29
-25
envelope.go
whisper/envelope.go
+6
-12
message.go
whisper/message.go
+12
-5
message_test.go
whisper/message_test.go
+8
-8
whisper.go
whisper/whisper.go
+3
-0
No files found.
whisper/envelope.go
View file @
7b501906
...
@@ -11,7 +11,6 @@ import (
...
@@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
)
)
...
@@ -85,27 +84,22 @@ func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
...
@@ -85,27 +84,22 @@ func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
}
}
data
=
data
[
1
:
]
data
=
data
[
1
:
]
if
message
.
Flags
&
128
==
128
{
if
message
.
Flags
&
signatureFlag
==
signatureFlag
{
if
len
(
data
)
<
65
{
if
len
(
data
)
<
signatureLength
{
return
nil
,
fmt
.
Errorf
(
"unable to open envelope. First bit set but len(data) <
65
"
)
return
nil
,
fmt
.
Errorf
(
"unable to open envelope. First bit set but len(data) <
len(signature)
"
)
}
}
message
.
Signature
,
data
=
data
[
:
65
],
data
[
65
:
]
message
.
Signature
,
data
=
data
[
:
signatureLength
],
data
[
signatureLength
:
]
}
}
message
.
Payload
=
data
message
.
Payload
=
data
//
Short circuit if the encryption was
requested
//
Decrypt the message, if
requested
if
key
==
nil
{
if
key
==
nil
{
return
message
,
nil
return
message
,
nil
}
}
// Otherwise try to decrypt the message
switch
message
.
decrypt
(
key
)
{
message
.
Payload
,
err
=
crypto
.
Decrypt
(
key
,
message
.
Payload
)
switch
err
{
case
nil
:
case
nil
:
return
message
,
nil
return
message
,
nil
case
ecies
.
ErrInvalidPublicKey
:
// Payload isn't encrypted
return
message
,
err
default
:
default
:
return
nil
,
fmt
.
Errorf
(
"unable to open envelope, decrypt failed: %v"
,
err
)
return
nil
,
fmt
.
Errorf
(
"unable to open envelope, decrypt failed: %v"
,
err
)
}
}
...
...
whisper/message.go
View file @
7b501906
...
@@ -35,8 +35,9 @@ type Options struct {
...
@@ -35,8 +35,9 @@ type Options struct {
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
func
NewMessage
(
payload
[]
byte
)
*
Message
{
func
NewMessage
(
payload
[]
byte
)
*
Message
{
// Construct an initial flag set: bit #1 = 0 (no signature), rest random
// Construct an initial flag set: no signature, rest random
flags
:=
byte
(
rand
.
Intn
(
128
))
flags
:=
byte
(
rand
.
Intn
(
256
))
flags
&=
^
signatureFlag
// Assemble and return the message
// Assemble and return the message
return
&
Message
{
return
&
Message
{
...
@@ -84,7 +85,7 @@ func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error)
...
@@ -84,7 +85,7 @@ func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error)
// sign calculates and sets the cryptographic signature for the message , also
// sign calculates and sets the cryptographic signature for the message , also
// setting the sign flag.
// setting the sign flag.
func
(
self
*
Message
)
sign
(
key
*
ecdsa
.
PrivateKey
)
(
err
error
)
{
func
(
self
*
Message
)
sign
(
key
*
ecdsa
.
PrivateKey
)
(
err
error
)
{
self
.
Flags
|=
1
<<
7
self
.
Flags
|=
signatureFlag
self
.
Signature
,
err
=
crypto
.
Sign
(
self
.
hash
(),
key
)
self
.
Signature
,
err
=
crypto
.
Sign
(
self
.
hash
(),
key
)
return
return
}
}
...
@@ -102,8 +103,14 @@ func (self *Message) Recover() *ecdsa.PublicKey {
...
@@ -102,8 +103,14 @@ func (self *Message) Recover() *ecdsa.PublicKey {
}
}
// encrypt encrypts a message payload with a public key.
// encrypt encrypts a message payload with a public key.
func
(
self
*
Message
)
encrypt
(
to
*
ecdsa
.
PublicKey
)
(
err
error
)
{
func
(
self
*
Message
)
encrypt
(
key
*
ecdsa
.
PublicKey
)
(
err
error
)
{
self
.
Payload
,
err
=
crypto
.
Encrypt
(
to
,
self
.
Payload
)
self
.
Payload
,
err
=
crypto
.
Encrypt
(
key
,
self
.
Payload
)
return
}
// decrypt decrypts an encrypted payload with a private key.
func
(
self
*
Message
)
decrypt
(
key
*
ecdsa
.
PrivateKey
)
(
err
error
)
{
self
.
Payload
,
err
=
crypto
.
Decrypt
(
key
,
self
.
Payload
)
return
return
}
}
...
...
whisper/message_test.go
View file @
7b501906
...
@@ -16,8 +16,8 @@ func TestMessageSimpleWrap(t *testing.T) {
...
@@ -16,8 +16,8 @@ func TestMessageSimpleWrap(t *testing.T) {
if
_
,
err
:=
msg
.
Wrap
(
DefaultProofOfWork
,
Options
{});
err
!=
nil
{
if
_
,
err
:=
msg
.
Wrap
(
DefaultProofOfWork
,
Options
{});
err
!=
nil
{
t
.
Fatalf
(
"failed to wrap message: %v"
,
err
)
t
.
Fatalf
(
"failed to wrap message: %v"
,
err
)
}
}
if
msg
.
Flags
&
128
!=
0
{
if
msg
.
Flags
&
signatureFlag
!=
0
{
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
(
msg
.
Flags
&
128
)
>>
7
,
0
)
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
msg
.
Flags
&
signatureFlag
,
0
)
}
}
if
len
(
msg
.
Signature
)
!=
0
{
if
len
(
msg
.
Signature
)
!=
0
{
t
.
Fatalf
(
"signature found for simple wrapping: 0x%x"
,
msg
.
Signature
)
t
.
Fatalf
(
"signature found for simple wrapping: 0x%x"
,
msg
.
Signature
)
...
@@ -41,8 +41,8 @@ func TestMessageCleartextSignRecover(t *testing.T) {
...
@@ -41,8 +41,8 @@ func TestMessageCleartextSignRecover(t *testing.T) {
});
err
!=
nil
{
});
err
!=
nil
{
t
.
Fatalf
(
"failed to sign message: %v"
,
err
)
t
.
Fatalf
(
"failed to sign message: %v"
,
err
)
}
}
if
msg
.
Flags
&
128
!=
128
{
if
msg
.
Flags
&
signatureFlag
!=
signatureFlag
{
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
(
msg
.
Flags
&
128
)
>>
7
,
1
)
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
msg
.
Flags
&
signatureFlag
,
signatureFlag
)
}
}
if
bytes
.
Compare
(
msg
.
Payload
,
payload
)
!=
0
{
if
bytes
.
Compare
(
msg
.
Payload
,
payload
)
!=
0
{
t
.
Fatalf
(
"payload mismatch after signing: have 0x%x, want 0x%x"
,
msg
.
Payload
,
payload
)
t
.
Fatalf
(
"payload mismatch after signing: have 0x%x, want 0x%x"
,
msg
.
Payload
,
payload
)
...
@@ -75,8 +75,8 @@ func TestMessageAnonymousEncryptDecrypt(t *testing.T) {
...
@@ -75,8 +75,8 @@ func TestMessageAnonymousEncryptDecrypt(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to encrypt message: %v"
,
err
)
t
.
Fatalf
(
"failed to encrypt message: %v"
,
err
)
}
}
if
msg
.
Flags
&
128
!=
0
{
if
msg
.
Flags
&
signatureFlag
!=
0
{
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
(
msg
.
Flags
&
128
)
>>
7
,
0
)
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
msg
.
Flags
&
signatureFlag
,
0
)
}
}
if
len
(
msg
.
Signature
)
!=
0
{
if
len
(
msg
.
Signature
)
!=
0
{
t
.
Fatalf
(
"signature found for anonymous message: 0x%x"
,
msg
.
Signature
)
t
.
Fatalf
(
"signature found for anonymous message: 0x%x"
,
msg
.
Signature
)
...
@@ -111,8 +111,8 @@ func TestMessageFullCrypto(t *testing.T) {
...
@@ -111,8 +111,8 @@ func TestMessageFullCrypto(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to encrypt message: %v"
,
err
)
t
.
Fatalf
(
"failed to encrypt message: %v"
,
err
)
}
}
if
msg
.
Flags
&
128
!=
128
{
if
msg
.
Flags
&
signatureFlag
!=
signatureFlag
{
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
(
msg
.
Flags
&
128
)
>>
7
,
1
)
t
.
Fatalf
(
"signature flag mismatch: have %d, want %d"
,
msg
.
Flags
&
signatureFlag
,
signatureFlag
)
}
}
if
len
(
msg
.
Signature
)
==
0
{
if
len
(
msg
.
Signature
)
==
0
{
t
.
Fatalf
(
"no signature found for signed message"
)
t
.
Fatalf
(
"no signature found for signed message"
)
...
...
whisper/whisper.go
View file @
7b501906
...
@@ -20,6 +20,9 @@ const (
...
@@ -20,6 +20,9 @@ const (
statusMsg
=
0x0
statusMsg
=
0x0
envelopesMsg
=
0x01
envelopesMsg
=
0x01
whisperVersion
=
0x02
whisperVersion
=
0x02
signatureFlag
=
byte
(
1
<<
7
)
signatureLength
=
65
)
)
type
MessageEvent
struct
{
type
MessageEvent
struct
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment