本文整理汇总了Golang中github.com/juju/utils.ReadSHA256函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadSHA256函数的具体用法?Golang ReadSHA256怎么用?Golang ReadSHA256使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadSHA256函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addCharm
func addCharm(st *state.State, curl *charm.URL, ch charm.Charm) (*state.Charm, error) {
var f *os.File
name := charm.Quote(curl.String())
switch ch := ch.(type) {
case *charm.CharmDir:
var err error
if f, err = ioutil.TempFile("", name); err != nil {
return nil, err
}
defer os.Remove(f.Name())
defer f.Close()
err = ch.ArchiveTo(f)
if err != nil {
return nil, fmt.Errorf("cannot bundle charm: %v", err)
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
case *charm.CharmArchive:
var err error
if f, err = os.Open(ch.Path); err != nil {
return nil, fmt.Errorf("cannot read charm bundle: %v", err)
}
defer f.Close()
default:
return nil, fmt.Errorf("unknown charm type %T", ch)
}
digest, size, err := utils.ReadSHA256(f)
if err != nil {
return nil, err
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
cfg, err := st.EnvironConfig()
if err != nil {
return nil, err
}
env, err := environs.New(cfg)
if err != nil {
return nil, err
}
stor := env.Storage()
if err := stor.Put(name, f, size); err != nil {
return nil, fmt.Errorf("cannot put charm: %v", err)
}
ustr, err := stor.URL(name)
if err != nil {
return nil, fmt.Errorf("cannot get storage URL for charm: %v", err)
}
u, err := url.Parse(ustr)
if err != nil {
return nil, fmt.Errorf("cannot parse storage URL: %v", err)
}
sch, err := st.AddCharm(ch, curl, u, digest)
if err != nil {
return nil, fmt.Errorf("cannot add charm: %v", err)
}
return sch, nil
}
示例2: assertUploaded
func (s *serviceSuite) assertUploaded(c *gc.C, storage statestorage.Storage, storagePath, expectedSHA256 string) {
reader, _, err := storage.Get(storagePath)
c.Assert(err, jc.ErrorIsNil)
defer reader.Close()
downloadedSHA256, _, err := utils.ReadSHA256(reader)
c.Assert(err, jc.ErrorIsNil)
c.Assert(downloadedSHA256, gc.Equals, expectedSHA256)
}
示例3: AssertCharmUploaded
func (s *RepoSuite) AssertCharmUploaded(c *gc.C, curl *charm.URL) {
ch, err := s.State.Charm(curl)
c.Assert(err, gc.IsNil)
url := ch.BundleURL()
resp, err := http.Get(url.String())
c.Assert(err, gc.IsNil)
defer resp.Body.Close()
digest, _, err := utils.ReadSHA256(resp.Body)
c.Assert(err, gc.IsNil)
c.Assert(ch.BundleSha256(), gc.Equals, digest)
}
示例4: addCharm
func (conn *Conn) addCharm(curl *charm.URL, ch charm.Charm) (*state.Charm, error) {
var f *os.File
name := charm.Quote(curl.String())
switch ch := ch.(type) {
case *charm.Dir:
var err error
if f, err = ioutil.TempFile("", name); err != nil {
return nil, err
}
defer os.Remove(f.Name())
defer f.Close()
err = ch.BundleTo(f)
if err != nil {
return nil, fmt.Errorf("cannot bundle charm: %v", err)
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
case *charm.Bundle:
var err error
if f, err = os.Open(ch.Path); err != nil {
return nil, fmt.Errorf("cannot read charm bundle: %v", err)
}
defer f.Close()
default:
return nil, fmt.Errorf("unknown charm type %T", ch)
}
digest, size, err := utils.ReadSHA256(f)
if err != nil {
return nil, err
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
stor := conn.Environ.Storage()
logger.Infof("writing charm to storage [%d bytes]", size)
if err := stor.Put(name, f, size); err != nil {
return nil, fmt.Errorf("cannot put charm: %v", err)
}
ustr, err := stor.URL(name)
if err != nil {
return nil, fmt.Errorf("cannot get storage URL for charm: %v", err)
}
u, err := url.Parse(ustr)
if err != nil {
return nil, fmt.Errorf("cannot parse storage URL: %v", err)
}
logger.Infof("adding charm to state")
sch, err := conn.State.AddCharm(ch, curl, u, digest)
if err != nil {
return nil, fmt.Errorf("cannot add charm: %v", err)
}
return sch, nil
}
示例5: addCharm
func addCharm(st *state.State, curl *charm.URL, ch charm.Charm) (*state.Charm, error) {
var f *os.File
name := charm.Quote(curl.String())
switch ch := ch.(type) {
case *charm.CharmDir:
var err error
if f, err = ioutil.TempFile("", name); err != nil {
return nil, err
}
defer os.Remove(f.Name())
defer f.Close()
err = ch.ArchiveTo(f)
if err != nil {
return nil, fmt.Errorf("cannot bundle charm: %v", err)
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
case *charm.CharmArchive:
var err error
if f, err = os.Open(ch.Path); err != nil {
return nil, fmt.Errorf("cannot read charm bundle: %v", err)
}
defer f.Close()
default:
return nil, fmt.Errorf("unknown charm type %T", ch)
}
digest, size, err := utils.ReadSHA256(f)
if err != nil {
return nil, err
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
stor := statestorage.NewStorage(st.ModelUUID(), st.MongoSession())
storagePath := fmt.Sprintf("/charms/%s-%s", curl.String(), digest)
if err := stor.Put(storagePath, f, size); err != nil {
return nil, fmt.Errorf("cannot put charm: %v", err)
}
info := state.CharmInfo{
Charm: ch,
ID: curl,
StoragePath: storagePath,
SHA256: digest,
}
sch, err := st.AddCharm(info)
if err != nil {
return nil, fmt.Errorf("cannot add charm: %v", err)
}
return sch, nil
}
示例6: step
func (s addCharm) step(c *gc.C, ctx *context) {
var buf bytes.Buffer
err := s.dir.ArchiveTo(&buf)
c.Assert(err, jc.ErrorIsNil)
body := buf.Bytes()
hash, _, err := utils.ReadSHA256(&buf)
c.Assert(err, jc.ErrorIsNil)
storagePath := fmt.Sprintf("/charms/%s/%d", s.dir.Meta().Name, s.dir.Revision())
ctx.charms[storagePath] = body
ctx.sch, err = ctx.st.AddCharm(s.dir, s.curl, storagePath, hash)
c.Assert(err, jc.ErrorIsNil)
}
示例7: AssertCharmUploaded
func (s *RepoSuite) AssertCharmUploaded(c *gc.C, curl *charm.URL) {
ch, err := s.State.Charm(curl)
c.Assert(err, jc.ErrorIsNil)
storage := storage.NewStorage(s.State.EnvironUUID(), s.State.MongoSession())
r, _, err := storage.Get(ch.StoragePath())
c.Assert(err, jc.ErrorIsNil)
defer r.Close()
digest, _, err := utils.ReadSHA256(r)
c.Assert(err, jc.ErrorIsNil)
c.Assert(ch.BundleSha256(), gc.Equals, digest)
}
示例8: download
// download fetches the supplied charm and checks that it has the correct sha256
// hash, then copies it into the directory. If a value is received on abort, the
// download will be stopped.
func (d *BundlesDir) download(info BundleInfo, abort <-chan struct{}) (err error) {
archiveURL, disableSSLHostnameVerification, err := info.ArchiveURL()
if err != nil {
return err
}
defer errors.Maskf(&err, "failed to download charm %q from %q", info.URL(), archiveURL)
dir := d.downloadsPath()
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
aurl := archiveURL.String()
logger.Infof("downloading %s from %s", info.URL(), aurl)
if disableSSLHostnameVerification {
logger.Infof("SSL hostname verification disabled")
}
dl := downloader.New(aurl, dir, disableSSLHostnameVerification)
defer dl.Stop()
for {
select {
case <-abort:
logger.Infof("download aborted")
return fmt.Errorf("aborted")
case st := <-dl.Done():
if st.Err != nil {
return st.Err
}
logger.Infof("download complete")
defer st.File.Close()
actualSha256, _, err := utils.ReadSHA256(st.File)
if err != nil {
return err
}
archiveSha256, err := info.ArchiveSha256()
if err != nil {
return err
}
if actualSha256 != archiveSha256 {
return fmt.Errorf(
"expected sha256 %q, got %q", archiveSha256, actualSha256,
)
}
logger.Infof("download verified")
if err := os.MkdirAll(d.path, 0755); err != nil {
return err
}
// Renaming an open file is not possible on Windows
st.File.Close()
return os.Rename(st.File.Name(), d.bundlePath(info))
}
}
}
示例9: download
// download fetches the supplied charm and checks that it has the correct sha256
// hash, then copies it into the directory. If a value is received on abort, the
// download will be stopped.
func (d *BundlesDir) download(info BundleInfo, abort <-chan struct{}) (err error) {
archiveURLs, err := info.ArchiveURLs()
if err != nil {
return errors.Annotatef(err, "failed to get download URLs for charm %q", info.URL())
}
defer errors.DeferredAnnotatef(&err, "failed to download charm %q from %q", info.URL(), archiveURLs)
dir := d.downloadsPath()
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
var st downloader.Status
for _, archiveURL := range archiveURLs {
aurl := archiveURL.String()
logger.Infof("downloading %s from %s", info.URL(), aurl)
st, err = tryDownload(aurl, dir, abort)
if err == nil {
break
}
}
if err != nil {
return err
}
logger.Infof("download complete")
defer st.File.Close()
actualSha256, _, err := utils.ReadSHA256(st.File)
if err != nil {
return err
}
archiveSha256, err := info.ArchiveSha256()
if err != nil {
return err
}
if actualSha256 != archiveSha256 {
return fmt.Errorf(
"expected sha256 %q, got %q", archiveSha256, actualSha256,
)
}
logger.Infof("download verified")
if err := os.MkdirAll(d.path, 0755); err != nil {
return err
}
// Renaming an open file is not possible on Windows
st.File.Close()
return os.Rename(st.File.Name(), d.bundlePath(info))
}
示例10: copyOneToolsPackage
// copyOneToolsPackage copies one tool from the source to the target.
func copyOneToolsPackage(tool *coretools.Tools, dest storage.Storage) error {
toolsName := envtools.StorageName(tool.Version)
logger.Infof("copying %v", toolsName)
resp, err := utils.GetValidatingHTTPClient().Get(tool.URL)
if err != nil {
return err
}
buf := &bytes.Buffer{}
srcFile := resp.Body
defer srcFile.Close()
tool.SHA256, tool.Size, err = utils.ReadSHA256(io.TeeReader(srcFile, buf))
if err != nil {
return err
}
sizeInKB := (tool.Size + 512) / 1024
logger.Infof("downloaded %v (%dkB), uploading", toolsName, sizeInKB)
logger.Infof("download %dkB, uploading", sizeInKB)
return dest.Put(toolsName, buf, tool.Size)
}
示例11: NewMockStore
// NewMockStore creates a mock charm store containing the specified charms.
func NewMockStore(c *gc.C, repo *Repo, charms map[string]int) *MockStore {
s := &MockStore{charms: charms, DefaultSeries: "precise"}
f, err := os.Open(repo.CharmArchivePath(c.MkDir(), "dummy"))
c.Assert(err, gc.IsNil)
defer f.Close()
buf := &bytes.Buffer{}
s.archiveSha256, _, err = utils.ReadSHA256(io.TeeReader(f, buf))
c.Assert(err, gc.IsNil)
s.archiveBytes = buf.Bytes()
c.Assert(err, gc.IsNil)
s.mux = http.NewServeMux()
s.mux.HandleFunc("/charm-info", s.serveInfo)
s.mux.HandleFunc("/charm-event", s.serveEvent)
s.mux.HandleFunc("/charm/", s.serveCharm)
lis, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, gc.IsNil)
s.listener = lis
go http.Serve(s.listener, s)
return s
}
示例12: fetchToolsArchive
func fetchToolsArchive(stor storage.StorageReader, toolsDir string, agentTools *tools.Tools) ([]byte, error) {
r, err := stor.Get(envtools.StorageName(agentTools.Version, toolsDir))
if err != nil {
return nil, err
}
defer r.Close()
var buf bytes.Buffer
hash, size, err := utils.ReadSHA256(io.TeeReader(r, &buf))
if err != nil {
return nil, err
}
if hash != agentTools.SHA256 {
return nil, errors.New("hash mismatch")
}
if size != agentTools.Size {
return nil, errors.New("size mismatch")
}
return buf.Bytes(), nil
}
示例13: copyOneToolsPackage
// copyOneToolsPackage copies one tool from the source to the target.
func copyOneToolsPackage(tools *coretools.Tools, u ToolsUploader) error {
toolsName := envtools.StorageName(tools.Version)
logger.Infof("downloading %v (%v)", toolsName, tools.URL)
resp, err := utils.GetValidatingHTTPClient().Get(tools.URL)
if err != nil {
return err
}
defer resp.Body.Close()
// Verify SHA-256 hash.
var buf bytes.Buffer
sha256, size, err := utils.ReadSHA256(io.TeeReader(resp.Body, &buf))
if err != nil {
return err
}
if tools.SHA256 == "" {
logger.Warningf("no SHA-256 hash for %v", tools.SHA256)
} else if sha256 != tools.SHA256 {
return errors.Errorf("SHA-256 hash mismatch (%v/%v)", sha256, tools.SHA256)
}
sizeInKB := (size + 512) / 1024
logger.Infof("uploading %v (%dkB) to environment", toolsName, sizeInKB)
return u.UploadTools(tools, buf.Bytes())
}
示例14: TestCommandString
func (*utilsSuite) TestCommandString(c *gc.C) {
type test struct {
args []string
expected string
}
tests := []test{
{nil, ""},
{[]string{"a"}, "a"},
{[]string{"a$"}, `"a\$"`},
{[]string{""}, ""},
{[]string{"\\"}, `"\\"`},
{[]string{"a", "'b'"}, "a 'b'"},
{[]string{"a b"}, `"a b"`},
{[]string{"a", `"b"`}, `a "\"b\""`},
{[]string{"a", `"b\"`}, `a "\"b\\\""`},
{[]string{"a\n"}, "\"a\n\""},
}
for i, test := range tests {
c.Logf("test %d: %q", i, test.args)
result := utils.CommandString(test.args...)
c.Assert(result, gc.Equals, test.expected)
}
}
func (*utilsSuite) TestReadSHA256AndReadFileSHA256(c *gc.C) {
sha256Tests := []struct {
content string
sha256 string
}{{
content: "",
sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}, {
content: "some content",
sha256: "290f493c44f5d63d06b374d0a5abd292fae38b92cab2fae5efefe1b0e9347f56",
}, {
content: "foo",
sha256: "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
}, {
content: "Foo",
sha256: "1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa",
}, {
content: "multi\nline\ntext\nhere",
sha256: "c384f11c0294280792a44d9d6abb81f9fd991904cb7eb851a88311b04114231e",
}}
tempDir := c.MkDir()
for i, test := range sha256Tests {
c.Logf("test %d: %q -> %q", i, test.content, test.sha256)
buf := bytes.NewBufferString(test.content)
hash, size, err := utils.ReadSHA256(buf)
c.Check(err, gc.IsNil)
c.Check(hash, gc.Equals, test.sha256)
c.Check(int(size), gc.Equals, len(test.content))
tempFileName := filepath.Join(tempDir, fmt.Sprintf("sha256-%d", i))
err = ioutil.WriteFile(tempFileName, []byte(test.content), 0644)
c.Check(err, gc.IsNil)
fileHash, fileSize, err := utils.ReadFileSHA256(tempFileName)
c.Check(err, gc.IsNil)
c.Check(fileHash, gc.Equals, hash)
c.Check(fileSize, gc.Equals, size)
}
}
示例15: AddCharmWithAuthorization
// AddCharmWithAuthorization adds the given charm URL (which must include revision) to
// the environment, if it does not exist yet. Local charms are not
// supported, only charm store URLs. See also AddLocalCharm().
//
// The authorization macaroon, args.CharmStoreMacaroon, may be
// omitted, in which case this call is equivalent to AddCharm.
func AddCharmWithAuthorization(st *state.State, args params.AddCharmWithAuthorization) error {
charmURL, err := charm.ParseURL(args.URL)
if err != nil {
return err
}
if charmURL.Schema != "cs" {
return fmt.Errorf("only charm store charm URLs are supported, with cs: schema")
}
if charmURL.Revision < 0 {
return fmt.Errorf("charm URL must include revision")
}
// First, check if a pending or a real charm exists in state.
stateCharm, err := st.PrepareStoreCharmUpload(charmURL)
if err != nil {
return err
}
if stateCharm.IsUploaded() {
// Charm already in state (it was uploaded already).
return nil
}
// Get the charm and its information from the store.
envConfig, err := st.EnvironConfig()
if err != nil {
return err
}
csURL, err := url.Parse(csclient.ServerURL)
if err != nil {
return err
}
csParams := charmrepo.NewCharmStoreParams{
URL: csURL.String(),
HTTPClient: httpbakery.NewHTTPClient(),
}
if args.CharmStoreMacaroon != nil {
// Set the provided charmstore authorizing macaroon
// as a cookie in the HTTP client.
// TODO discharge any third party caveats in the macaroon.
ms := []*macaroon.Macaroon{args.CharmStoreMacaroon}
httpbakery.SetCookie(csParams.HTTPClient.Jar, csURL, ms)
}
repo := config.SpecializeCharmRepo(
NewCharmStore(csParams),
envConfig,
)
downloadedCharm, err := repo.Get(charmURL)
if err != nil {
cause := errors.Cause(err)
if httpbakery.IsDischargeError(cause) || httpbakery.IsInteractionError(cause) {
return errors.NewUnauthorized(err, "")
}
return errors.Trace(err)
}
// Open it and calculate the SHA256 hash.
downloadedBundle, ok := downloadedCharm.(*charm.CharmArchive)
if !ok {
return errors.Errorf("expected a charm archive, got %T", downloadedCharm)
}
archive, err := os.Open(downloadedBundle.Path)
if err != nil {
return errors.Annotate(err, "cannot read downloaded charm")
}
defer archive.Close()
bundleSHA256, size, err := utils.ReadSHA256(archive)
if err != nil {
return errors.Annotate(err, "cannot calculate SHA256 hash of charm")
}
if _, err := archive.Seek(0, 0); err != nil {
return errors.Annotate(err, "cannot rewind charm archive")
}
// Store the charm archive in environment storage.
return StoreCharmArchive(
st,
charmURL,
downloadedCharm,
archive,
size,
bundleSHA256,
)
}