...
 
Commits (33)
    https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/ee302b211dd9abdbca5de9f96aee10d502a5ad49 import from go-ipfs 2015-11-15T20:20:54-08:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/8b6cc6f134aa799cc9b939c0707a31cd7fe3e533 gx-ify 2015-11-15T21:05:44-08:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/9814eaec2c59803d85016286c561010345e98f05 util: Add DefaultIpfsHash constant for programtically accessing the current d... 2016-03-16T18:51:15+01:00 Chris P sahib@online.de https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/d6aa22b9506c5587fc5c20489c0d96463569f8d0 license 2016-03-22T09:02:29-04:00 jbenet juan@benet.ai https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/6b4d6f2f4f5d429d011ba3133de8fee75b27584a Merge pull request #1 from disorganizer/master 2016-04-28T13:11:44-07:00 Jeromy Johnson why@ipfs.io util: Add DefaultIpfsHash constant for programtically accessing the c… https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/445bfb31dda5736bfbe206b693c7c3527a167d99 Standardized Readme 2016-06-21T13:20:31+01:00 Richard Littauer richard.littauer@gmail.com See <a href="https://github.com/ipfs/community/issues/124" rel="nofollow noreferrer noopener" target="_blank">https://github.com/ipfs/community/issues/124</a> https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/b87e38d7a44d24a0d2d1ee53c4ae2348e831fd41 Merge pull request #2 from ipfs/feature/standardize-readme 2016-06-21T12:18:02-07:00 Jeromy Johnson why@ipfs.io Standardized Readme https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/03f76a71a0fc8a58f483f4725d02a442cbea1953 gx publish 2.0.0 2016-10-04T18:21:19-07:00 Jeromy why@ipfs.io https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/70eede8dce2a98e86bb760247702e4e9b23139bb gx publish 1.2.0 2016-10-04T18:21:48-07:00 Jeromy why@ipfs.io https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/77a8e06728e8f7d285652b6deeff73ab4a445bf0 update go-multihash with blake and dblsha 2017-02-01T17:45:18-08:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/78188a11e9b4e58e58d37b124fd43afcbef90ec8 gx publish 1.2.1 2017-02-01T17:59:50-08:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/5e06988d92bcaaae765b36db9b70de3afc66418f Docs: Improve Readme and make golint happy 2017-03-17T15:34:11+01:00 Hector Sanjuan code@hector.link License: MIT Signed-off-by: <span data-trailer="Signed-off-by:"><a href="mailto:code@hector.link" title="code@hector.link"><img alt="default avatar" src="https://secure.gravatar.com/avatar/156aee81bc873ecbc31bc099451e5afd?s=32&amp;d=identicon" class="avatar s16 avatar-inline"></a><a href="mailto:code@hector.link" title="code@hector.link">Hector Sanjuan</a> &lt;<a href="mailto:code@hector.link" title="code@hector.link">code@hector.link</a>&gt;</span> https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/50c559d0e50bb0aea8397dfca408be8b6fcdd86c gx publish 1.2.3 2017-03-21T12:22:24-07:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/6a4d34b01bc6bb090a3fe06d2bc02655c4a711f6 gx publish 1.2.4 2017-03-23T18:22:29-07:00 Jeromy jeromyj@gmail.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/f25fcc891281327394bb48000ef0970d11baff2b Merge pull request #3 from ipfs/docs-improvements 2017-03-28T21:32:45+02:00 Jakub Sztandera kubuxu@protonmail.ch Docs: Improve Readme and make golint happy https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/23f8455e09816f389192b7fc47d30dcfcf09b938 gx publish 1.2.5 2017-07-10T14:43:14-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/ca91b45d2e776e6e066151f7b65a3984c87e9fbb Merge pull request #4 from ipfs/gx/publish-1.2.5 2017-07-10T15:01:38-07:00 Steven Allen steven@stebalien.com gx publish 1.2.5 https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/ecb455b230ce7cb40510186fa723aa19af133aaf gx publish 1.2.6 2017-12-04T23:38:11+00:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/a5ec80aed64a2438aee9a0a4a4c1a9140c0db273 switch to a faster b58 library 2017-12-18T10:37:12-08:00 Steven Allen steven@stebalien.com and update go-multihash https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/6ef617de71fd876f13bfcb466083c400954f4cbc Merge pull request #7 from ipfs/feat/faster-b58 2017-12-18T18:41:01+00:00 Steven Allen steven@stebalien.com switch to a faster b58 library https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/9ed527918c2f20abdf0adfab0553cd87db34f656 gx publish 1.2.7 2018-01-02T13:56:04-08:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/10d786c5ed859afd22223df76a89bf57b24b2ee1 gx publish 1.2.8 2018-06-08T20:23:37-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/05b6094b6fa9c1e49b4b941061fdc147db1a21b7 gx publish 1.2.9 2018-11-02T16:33:36-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/a4bb5361e49427531f9a716ead2ce4bd9bdd7959 Add gomod and travis 2019-02-28T18:49:28+01:00 Jakub Sztandera kubuxu@protonmail.ch https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/6b60f8d74b09bac9d7bef7a7ffb795efff749c0a doc: add a lead maintainer 2019-09-18T22:00:46-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/5df41752147cfd90051855e38df986c14b0b66be Add standard issue template 2020-04-27T11:22:36+02:00 Hector Sanjuan code@hector.link https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/499a8184a808c569d6a0883bc51b735429c92c72 Add autocomment configuration 2020-05-04T12:02:25+02:00 Hector Sanjuan code@hector.link https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/b315fdee43ac4019a9e9a948dcf2f3f2942a04ab chore: update deps 2020-06-05T20:39:04-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/f847294507dd772eb85037bf50b785bef1037bb7 chore(ci): bump go version 2020-06-05T20:41:23-07:00 Steven Allen steven@stebalien.com https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/f4952d1c5a6a9431d6a4d5ea57c4818e6d199464 Merge pull request #8 from ipfs/chore/update-deps 2020-06-05T20:46:58-07:00 Steven Allen steven@stebalien.com chore: update deps https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/ffbf0ba0dd4bc93d42c2708e45a798909f00e0a0 Merge remote-tracking branch 'upstream/master' into reference 2021-04-20T01:27:07-04:00 tavit ohanian tavit@dms3.io https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/102a4e1a68d62a3eba878907285342d5e0a32d45 dms3 initial variant 2021-05-17T11:07:22-04:00 tavit ohanian tavit@dms3.io https://gitlab.dms3.io/dms3/go-dms3-util/-/commit/cb085f6edfe8271432940d69295c2ff57f138be2 Merge branch 'port-2021-04-20' into 'master' 2021-05-17T15:08:54+00:00 tavit ohanian tavit@dms3.io Port 2021 04 20 See merge request dms3/public/go-dms3-util!1
blank_issues_enabled: false
contact_links:
- name: Getting Help on IPFS
url: https://ipfs.io/help
about: All information about how and where to get help on IPFS.
- name: IPFS Official Forum
url: https://discuss.ipfs.io
about: Please post general questions, support requests, and discussions here.
---
name: Open an issue
about: Only for actionable issues relevant to this repository.
title: ''
labels: need/triage
assignees: ''
---
<!--
Hello! To ensure this issue is correctly addressed as soon as possible by the IPFS team, please try to make sure:
- This issue is relevant to this repository's topic or codebase.
- A clear description is provided. It should includes as much relevant information as possible and clear scope for the issue to be actionable.
FOR GENERAL DISCUSSION, HELP OR QUESTIONS, please see the options at https://ipfs.io/help or head directly to https://discuss.ipfs.io.
(you can delete this section after reading)
-->
# Configuration for welcome - https://github.com/behaviorbot/welcome
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
# Comment to be posted to on first time issues
newIssueWelcomeComment: >
Thank you for submitting your first issue to this repository! A maintainer
will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the
necessary information to make this process easy! Any information that can
help save additional round trips is useful! We currently aim to give
initial feedback within **two business days**. If this does not happen, feel
free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an
overview of priorities, assignments and additional actions requested by the
maintainers:
- "Priority" labels will show how urgent this is for the team.
- "Status" labels will show if this is ready to be worked on, blocked, or in progress.
- "Need" labels will indicate if additional input or analysis is required.
Finally, remember to use https://discuss.ipfs.io if you just need general
support.
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
# Comment to be posted to on PRs from first time contributors in your repository
newPRWelcomeComment: >
Thank you for submitting this PR!
A maintainer will be here shortly to review it.
We are super grateful, but we are also overloaded! Help us by making sure
that:
* The context for this PR is clear, with relevant discussion, decisions
and stakeholders linked/mentioned.
* Your contribution itself is clear (code comments, self-review for the
rest) and in its best form. Follow the [code contribution
guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines)
if they apply.
Getting other community members to do a review would be great help too on
complex PRs (you can ask in the chats/forums). If you are unsure about
something, just leave us a comment.
Next steps:
* A maintainer will triage and assign priority to this PR, commenting on
any missing things and potentially assigning a reviewer for high
priority items.
* The PR gets reviews, discussed and approvals as needed.
* The PR is merged by maintainers when it has been approved and comments addressed.
We currently aim to provide initial feedback/triaging within **two business
days**. Please keep an eye on any labelling actions, as these will indicate
priorities and status of your contribution.
We are very grateful for your contribution!
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
# Comment to be posted to on pull requests merged by a first time user
# Currently disabled
#firstPRMergeComment: ""
1.2.9: QmNohiVssaPw3KVLZik59DBVGTSm2dGvYT9eoXt5DQ36Yz
os:
- linux
language: go
go:
- 1.14.x
env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod
# disable travis install
install:
- true
script:
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
cache:
directories:
- $GOPATH/src/gx
- $GOPATH/pkg/mod
- $HOME/.cache/go-build
notifications:
email: false
The MIT License (MIT)
Copyright (c) 2016 Juan Batiz-Benet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# go-dms3-util
# go-ipfs-util
dms3 go util
\ No newline at end of file
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![](https://img.shields.io/badge/discussion_repo-go_to_issues-brightgreen.svg?style=flat-square)](https://github.com/ipfs/NAME/issues)
> Common utilities used by go-ipfs and other related go packages
## Lead Maintainer
[Steven Allen](https://github.com/Stebalien)
## Install
This is a Go module which can be installed with `go get github.com/ipfs/go-ipfs-util`. `go-ipfs-util` is however packaged with Gx, so it is recommended to use Gx to install it (see Usage section).
## Usage
This module is packaged with [Gx](https://github.com/whyrusleeping/gx).
In order to use it in your own project do:
```
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
cd <your-project-repository>
gx init
gx import github.com/ipfs/go-ipfs-util
gx install --global
gx-go --rewrite
```
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
## Contribute
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/go-ipfs-util/issues)!
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
### Want to hack on IPFS?
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md)
## License
MIT
package util
import "os"
// FileExists check if the file with the given path exits.
func FileExists(filename string) bool {
fi, err := os.Lstat(filename)
if fi != nil || (err != nil && !os.IsNotExist(err)) {
return true
}
return false
}
package util
import "testing"
func TestFileDoesNotExist(t *testing.T) {
t.Parallel()
if FileExists("i would be surprised to discover that this file exists") {
t.Fail()
}
}
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
{
"author": "whyrusleeping",
"bugs": {},
"gx": {
"dvcsimport": "github.com/ipfs/go-ipfs-util"
},
"gxDependencies": [
{
"author": "multiformats",
"hash": "QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW",
"name": "go-multihash",
"version": "1.0.9"
},
{
"author": "mr-tron",
"hash": "QmWFAMPqsEyUX7gDUsRVmMWz59FxSpJ1b2v6bJ1yYzo7jY",
"name": "go-base58-fast",
"version": "0.1.1"
}
],
"gxVersion": "0.9.1",
"language": "go",
"license": "",
"name": "go-ipfs-util",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "1.2.9"
}
package util
import "time"
// TimeFormatDms3 is the format dms3 uses to represent time in string form.
var TimeFormatDms3 = time.RFC3339Nano
// ParseRFC3339 parses an RFC3339Nano-formatted time stamp and
// returns the UTC time.
func ParseRFC3339(s string) (time.Time, error) {
t, err := time.Parse(TimeFormatDms3, s)
if err != nil {
return time.Time{}, err
}
return t.UTC(), nil
}
// FormatRFC3339 returns the string representation of the
// UTC value of the given time in RFC3339Nano format.
func FormatRFC3339(t time.Time) string {
return t.UTC().Format(TimeFormatDms3)
}
package util
import (
"testing"
"time"
)
func TestTimeFormatParseInversion(t *testing.T) {
v, err := ParseRFC3339(FormatRFC3339(time.Now()))
if err != nil {
t.Fatal(err)
}
if v.Location() != time.UTC {
t.Fatal("Time should be UTC")
}
}
// Package util implements various utility functions used within dms3
// that do not currently have a better place to live.
package util
import (
"errors"
"io"
"math/rand"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
b58 "github.com/mr-tron/base58/base58"
mh "github.com/multiformats/go-multihash"
)
// DefaultDms3Hash is the current default hash function used by DMS3.
const DefaultDms3Hash = mh.SHA2_256
// Debug is a global flag for debugging.
var Debug bool
// ErrNotImplemented signifies a function has not been implemented yet.
var ErrNotImplemented = errors.New("Error: not implemented yet.")
// ErrTimeout implies that a timeout has been triggered
var ErrTimeout = errors.New("Error: Call timed out.")
// ErrSearchIncomplete implies that a search type operation didnt
// find the expected node, but did find 'a' node.
var ErrSearchIncomplete = errors.New("Error: Search Incomplete.")
// ErrCast is returned when a cast fails AND the program should not panic.
func ErrCast() error {
debug.PrintStack()
return errCast
}
var errCast = errors.New("cast error")
// ExpandPathnames takes a set of paths and turns them into absolute paths
func ExpandPathnames(paths []string) ([]string, error) {
var out []string
for _, p := range paths {
abspath, err := filepath.Abs(p)
if err != nil {
return nil, err
}
out = append(out, abspath)
}
return out, nil
}
type randGen struct {
rand.Rand
}
// NewTimeSeededRand returns a random bytes reader
// which has been initialized with the current time.
func NewTimeSeededRand() io.Reader {
src := rand.NewSource(time.Now().UnixNano())
return &randGen{
Rand: *rand.New(src),
}
}
// NewSeededRand returns a random bytes reader
// initialized with the given seed.
func NewSeededRand(seed int64) io.Reader {
src := rand.NewSource(seed)
return &randGen{
Rand: *rand.New(src),
}
}
func (r *randGen) Read(p []byte) (n int, err error) {
for i := 0; i < len(p); i++ {
p[i] = byte(r.Rand.Intn(255))
}
return len(p), nil
}
// GetenvBool is the way to check an env var as a boolean
func GetenvBool(name string) bool {
v := strings.ToLower(os.Getenv(name))
return v == "true" || v == "t" || v == "1"
}
// MultiErr is a util to return multiple errors
type MultiErr []error
func (m MultiErr) Error() string {
if len(m) == 0 {
return "no errors"
}
s := "Multiple errors: "
for i, e := range m {
if i != 0 {
s += ", "
}
s += e.Error()
}
return s
}
// Partition splits a subject 3 parts: prefix, separator, suffix.
// The first occurrence of the separator will be matched.
// ie. Partition("Ready, steady, go!", ", ") -> ["Ready", ", ", "steady, go!"]
func Partition(subject string, sep string) (string, string, string) {
if i := strings.Index(subject, sep); i != -1 {
return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):]
}
return subject, "", ""
}
// RPartition splits a subject 3 parts: prefix, separator, suffix.
// The last occurrence of the separator will be matched.
// ie. RPartition("Ready, steady, go!", ", ") -> ["Ready, steady", ", ", "go!"]
func RPartition(subject string, sep string) (string, string, string) {
if i := strings.LastIndex(subject, sep); i != -1 {
return subject[:i], subject[i : i+len(sep)], subject[i+len(sep):]
}
return subject, "", ""
}
// Hash is the global DMS3 hash function. uses multihash SHA2_256, 256 bits
func Hash(data []byte) mh.Multihash {
h, err := mh.Sum(data, DefaultDms3Hash, -1)
if err != nil {
// this error can be safely ignored (panic) because multihash only fails
// from the selection of hash function. If the fn + length are valid, it
// won't error.
panic("multihash failed to hash using SHA2_256.")
}
return h
}
// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
func IsValidHash(s string) bool {
out, err := b58.Decode(s)
if err != nil {
return false
}
_, err = mh.Cast(out)
return err == nil
}
// XOR takes two byte slices, XORs them together, returns the resulting slice.
func XOR(a, b []byte) []byte {
c := make([]byte, len(a))
for i := 0; i < len(a); i++ {
c[i] = a[i] ^ b[i]
}
return c
}
package util
import (
"bytes"
"testing"
)
func TestXOR(t *testing.T) {
cases := [][3][]byte{
{
{0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF},
{0x00, 0x00, 0x00},
},
{
{0x00, 0xFF, 0x00},
{0xFF, 0xFF, 0xFF},
{0xFF, 0x00, 0xFF},
},
{
{0x55, 0x55, 0x55},
{0x55, 0xFF, 0xAA},
{0x00, 0xAA, 0xFF},
},
}
for _, c := range cases {
r := XOR(c[0], c[1])
if !bytes.Equal(r, c[2]) {
t.Error("XOR failed")
}
}
}
func BenchmarkHash256K(b *testing.B) {
buf := make([]byte, 256*1024)
NewTimeSeededRand().Read(buf)
b.SetBytes(int64(256 * 1024))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Hash(buf)
}
}
func BenchmarkHash512K(b *testing.B) {
buf := make([]byte, 512*1024)
NewTimeSeededRand().Read(buf)
b.SetBytes(int64(512 * 1024))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Hash(buf)
}
}
func BenchmarkHash1M(b *testing.B) {
buf := make([]byte, 1024*1024)
NewTimeSeededRand().Read(buf)
b.SetBytes(int64(1024 * 1024))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Hash(buf)
}
}