Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
9267f450
提交
9267f450
authored
12月 12, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
secio: encrypt copy
sadly, encrypting needs to copy, as the user supplied buffer must not be mangled.
上级
8d961fc0
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
26 行增加
和
12 行删除
+26
-12
rw.go
crypto/secio/rw.go
+26
-12
没有找到文件。
crypto/secio/rw.go
浏览文件 @
9267f450
...
...
@@ -11,21 +11,27 @@ import (
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
msgio
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
mpool
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio/mpool"
)
// ErrMACInvalid signals that a MAC verification failed
var
ErrMACInvalid
=
errors
.
New
(
"MAC verification failed"
)
// BufPool is a ByteSlicePool for messages. we need buffers because (sadly)
// we cannot encrypt in place-- the user needs their buffer back.
var
BufPool
=
mpool
.
ByteSlicePool
type
etmWriter
struct
{
// params
msg
msgio
.
WriteCloser
str
cipher
.
Stream
mac
HMAC
pool
mpool
.
Pool
// for the buffers with encrypted data
msg
msgio
.
WriteCloser
// msgio for knowing where boundaries lie
str
cipher
.
Stream
// the stream cipher to encrypt with
mac
HMAC
// the mac to authenticate data with
}
// NewETMWriter Encrypt-Then-MAC
func
NewETMWriter
(
w
io
.
Writer
,
s
cipher
.
Stream
,
mac
HMAC
)
msgio
.
WriteCloser
{
return
&
etmWriter
{
msg
:
msgio
.
NewWriter
(
w
),
str
:
s
,
mac
:
mac
}
return
&
etmWriter
{
msg
:
msgio
.
NewWriter
(
w
),
str
:
s
,
mac
:
mac
,
pool
:
BufPool
}
}
// Write writes passed in buffer as a single message.
...
...
@@ -40,21 +46,26 @@ func (w *etmWriter) Write(b []byte) (int, error) {
func
(
w
*
etmWriter
)
WriteMsg
(
b
[]
byte
)
error
{
// encrypt.
w
.
str
.
XORKeyStream
(
b
,
b
)
data
:=
w
.
pool
.
Get
(
uint32
(
len
(
b
)))
.
([]
byte
)
data
=
data
[
:
len
(
b
)]
// the pool's buffer may be larger
w
.
str
.
XORKeyStream
(
data
,
b
)
// log.Debugf("ENC plaintext (%d): %s %v", len(b), b, b)
// log.Debugf("ENC ciphertext (%d): %s %v", len(data), data, data)
// then, mac.
if
_
,
err
:=
w
.
mac
.
Write
(
b
);
err
!=
nil
{
if
_
,
err
:=
w
.
mac
.
Write
(
data
);
err
!=
nil
{
return
err
}
// Sum appends.
b
=
w
.
mac
.
Sum
(
b
)
data
=
w
.
mac
.
Sum
(
data
)
w
.
mac
.
Reset
()
// it's sad to append here. our buffers are -- hopefully -- coming from
// a shared buffer pool, so the append may not actually cause allocation
// one can only hope. i guess we'll see.
return
w
.
msg
.
WriteMsg
(
b
)
return
w
.
msg
.
WriteMsg
(
data
)
}
func
(
w
*
etmWriter
)
Close
()
error
{
...
...
@@ -66,9 +77,9 @@ type etmReader struct {
io
.
Closer
// params
msg
msgio
.
ReadCloser
str
cipher
.
Stream
mac
HMAC
msg
msgio
.
ReadCloser
// msgio for knowing where boundaries lie
str
cipher
.
Stream
// the stream cipher to encrypt with
mac
HMAC
// the mac to authenticate data with
}
// NewETMReader Encrypt-Then-MAC
...
...
@@ -137,8 +148,11 @@ func (r *etmReader) macCheckThenDecrypt(m []byte) (int, error) {
return
0
,
ErrMACInvalid
}
// ok seems good. decrypt.
// ok seems good. decrypt. (can decrypt in place, yay!)
// log.Debugf("DEC ciphertext (%d): %s %v", len(data), data, data)
r
.
str
.
XORKeyStream
(
data
,
data
)
// log.Debugf("DEC plaintext (%d): %s %v", len(data), data, data)
return
mark
,
nil
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论