Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-cid
Commits
5d8ad3eb
Commit
5d8ad3eb
authored
Jun 28, 2018
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create new Format interface for creating CIDs.
parent
5b04f304
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
116 additions
and
0 deletions
+116
-0
cid.go
cid.go
+3
-0
format.go
format.go
+55
-0
format_test.go
format_test.go
+58
-0
No files found.
cid.go
View file @
5d8ad3eb
...
...
@@ -145,6 +145,7 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) *Cid {
}
// NewPrefixV0 returns a CIDv0 prefix with the specified multihash type.
// DEPRECATED: Use FormatV0
func
NewPrefixV0
(
mhType
uint64
)
Prefix
{
return
Prefix
{
MhType
:
mhType
,
...
...
@@ -156,6 +157,7 @@ func NewPrefixV0(mhType uint64) Prefix {
// NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash
// type.
// DEPRECATED: Use FormatV1
func
NewPrefixV1
(
codecType
uint64
,
mhType
uint64
)
Prefix
{
return
Prefix
{
MhType
:
mhType
,
...
...
@@ -451,6 +453,7 @@ type Prefix struct {
// Sum uses the information in a prefix to perform a multihash.Sum()
// and return a newly constructed Cid with the resulting multihash.
// DEPRECATED: Use PrefixToFormat(p).Sum(data)
func
(
p
Prefix
)
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
{
hash
,
err
:=
mh
.
Sum
(
data
,
p
.
MhType
,
p
.
MhLength
)
if
err
!=
nil
{
...
...
format.go
0 → 100644
View file @
5d8ad3eb
package
cid
import
(
mh
"github.com/multiformats/go-multihash"
)
type
Format
interface
{
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
}
type
FormatV0
struct
{}
type
FormatV1
struct
{
Codec
uint64
HashFun
uint64
HashLen
int
// HashLen <= 0 means the default length
}
func
PrefixToFormat
(
p
Prefix
)
Format
{
if
p
.
Version
==
0
{
return
FormatV0
{}
}
mhLen
:=
p
.
MhLength
if
p
.
MhType
==
mh
.
ID
{
mhLen
=
0
}
if
mhLen
<
0
{
mhLen
=
0
}
return
FormatV1
{
Codec
:
p
.
Codec
,
HashFun
:
p
.
MhType
,
HashLen
:
mhLen
,
}
}
func
(
p
FormatV0
)
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
{
hash
,
err
:=
mh
.
Sum
(
data
,
mh
.
SHA2_256
,
-
1
)
if
err
!=
nil
{
return
nil
,
err
}
return
NewCidV0
(
hash
),
nil
}
func
(
p
FormatV1
)
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
{
mhLen
:=
p
.
HashLen
if
mhLen
<=
0
{
mhLen
=
-
1
}
hash
,
err
:=
mh
.
Sum
(
data
,
p
.
HashFun
,
mhLen
)
if
err
!=
nil
{
return
nil
,
err
}
return
NewCidV1
(
p
.
Codec
,
hash
),
nil
}
format_test.go
0 → 100644
View file @
5d8ad3eb
package
cid
import
(
"testing"
mh
"github.com/multiformats/go-multihash"
)
func
TestFormatV1
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"this is some test content"
)
// Construct c1
format
:=
FormatV1
{
Codec
:
DagCBOR
,
HashFun
:
mh
.
SHA2_256
}
c1
,
err
:=
format
.
Sum
(
data
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// Construct c2
hash
,
err
:=
mh
.
Sum
(
data
,
mh
.
SHA2_256
,
-
1
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c2
:=
NewCidV1
(
DagCBOR
,
hash
)
if
!
c1
.
Equals
(
c2
)
{
t
.
Fatal
(
"cids mismatch"
)
}
if
c1
.
Prefix
()
!=
c2
.
Prefix
()
{
t
.
Fatal
(
"prefixes mismatch"
)
}
}
func
TestFormatV0
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"this is some test content"
)
// Construct c1
format
:=
FormatV0
{}
c1
,
err
:=
format
.
Sum
(
data
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// Construct c2
hash
,
err
:=
mh
.
Sum
(
data
,
mh
.
SHA2_256
,
-
1
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c2
:=
NewCidV0
(
hash
)
if
!
c1
.
Equals
(
c2
)
{
t
.
Fatal
(
"cids mismatch"
)
}
if
c1
.
Prefix
()
!=
c2
.
Prefix
()
{
t
.
Fatal
(
"prefixes mismatch"
)
}
}
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