• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang sha1.Write函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中crypto/sha1.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Write函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: getScramble

func getScramble(keyA *big.Int, keyB *big.Int) *big.Int {
	// keyA:A client public ephemeral values
	// keyB:B server public ephemeral values

	sha1 := sha1.New()
	sha1.Write(pad(keyA))
	sha1.Write(pad(keyB))

	return bytesToBig(sha1.Sum(nil))
}
开发者ID:kleopatra999,项目名称:firebirdsql,代码行数:10,代码来源:srp.go


示例2: main

func main() {
	fmt.Println("Start...")
	// String from:
	// http://en.wikipedia.org/wiki/WebSocket
	s := "x3JJHMbDL1EzLkh9GBhXDw==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

	// sha1 stuff ...
	sha1 := sha1.New()
	sha1.Write([]byte(s))
	ss := fmt.Sprintf("%x", sha1.Sum(nil))
	fmt.Printf("%s\n", ss)
	w := "1d29ab734b0c9585240069a6e4e3e91b61da1969"
	fmt.Printf("%s\n", w)
	if ss != w {
		panic("Uh oh, something is not right")
	}
	// ---------------------------------------------------------------------------
	// The base64 encoding part of that post is left as an exercise for now.
	// From the article the base64 result should be:
	// HSmrc0sMlYUkAGmm5OPpG2HaGWk=
	// *not tested*
	// ---------------------------------------------------------------------------
	// sha256 stuff ...
	sha256 := sha256.New()
	sha256.Write([]byte(s))
	ss = fmt.Sprintf("%x", sha256.Sum(nil))
	fmt.Printf("%s\n", ss)
	fmt.Println("End...")
}
开发者ID:ra,项目名称:go-samp,代码行数:29,代码来源:shax.go


示例3: CalcStrong

// Calculate the strong checksum of a directory.
func CalcStrong(dir Dir) string {
	var sha1 = sha1.New()
	//	s := reprDir(dir)
	//	fmt.Printf("%s\n", s)
	//	sha1.Write(s)
	sha1.Write(reprDir(dir))
	return toHexString(sha1)
}
开发者ID:cmars,项目名称:replican-sync,代码行数:9,代码来源:node.go


示例4: IndexFile

// Build a hierarchical tree model representing a file's contents
func IndexFile(path string) (file *File, err error) {
	var f *os.File
	var buf [BLOCKSIZE]byte

	stat, err := os.Stat(path)
	if stat == nil {
		return nil, err
	} else if !stat.IsRegular() {
		return nil, errors.New(fmt.Sprintf("%s: not a regular file", path))
	}

	f, err = os.Open(path)
	if f == nil {
		return nil, err
	}
	defer f.Close()

	file = new(File)
	_, basename := filepath.Split(path)
	file.name = basename
	file.mode = stat.Mode

	if fileInfo, err := f.Stat(); fileInfo != nil {
		file.Size = fileInfo.Size
	} else {
		return nil, err
	}

	var block *Block
	sha1 := sha1.New()
	blockNum := 0

	for {
		switch rd, err := f.Read(buf[:]); true {
		case rd < 0:
			return nil, err
		case rd == 0:
			file.strong = toHexString(sha1)
			return file, nil
		case rd > 0:
			// Update block hashes
			block = IndexBlock(buf[0:rd])
			block.position = blockNum
			file.Blocks = append(file.Blocks, block)

			// update file hash
			sha1.Write(buf[0:rd])

			// Increment block counter
			blockNum++
		}
	}

	return nil, nil
}
开发者ID:vanackere,项目名称:replican-sync,代码行数:56,代码来源:index.go


示例5: b64sha1

func b64sha1(data []byte) string {
	sha1 := sha1.New()
	sha1.Write(data)
	hash := sha1.Sum(nil)

	b := new(bytes.Buffer)
	b64 := base64.NewEncoder(base64.URLEncoding, b)
	b64.Write(hash)

	return b.String()
}
开发者ID:kylelemons,项目名称:gopaste,代码行数:11,代码来源:paste.go


示例6: generateId

// Generate a unique id as a base64 encoded string
func generateId() string {

	// Generate a random 128 bit Id
	z, err := rand.Int(rand.Reader, maxSessionId)
	if err != nil {
		log.Fatal("Cannot generate session id", err)
	}

	// Take the hash of the random id
	sha1 := sha1.New()
	sha1.Write(z.Bytes())

	return base64.StdEncoding.EncodeToString(sha1.Sum([]byte{}))
}
开发者ID:alienscience,项目名称:bread,代码行数:15,代码来源:cookie.go


示例7: IndexFile

// Build a hierarchical tree model representing a file's contents
func IndexFile(path string) (fileInfo *FileInfo, blocksInfo []*BlockInfo, err os.Error) {
	var f *os.File
	var buf [BLOCKSIZE]byte

	stat, err := os.Stat(path)
	if stat == nil {
		return nil, nil, err
	} else if !stat.IsRegular() {
		return nil, nil, os.NewError(fmt.Sprintf("%s: not a regular file", path))
	}

	f, err = os.Open(path)
	if f == nil {
		return nil, nil, err
	}
	defer f.Close()

	_, basename := filepath.Split(path)
	fileInfo = &FileInfo{
		Name: basename,
		Mode: stat.Mode,
		Size: stat.Size}

	var block *BlockInfo
	sha1 := sha1.New()
	blockNum := 0
	blocksInfo = []*BlockInfo{}

	for {
		switch rd, err := f.Read(buf[:]); true {
		case rd < 0:
			return nil, nil, err
		case rd == 0:
			fileInfo.Strong = toHexString(sha1)
			return fileInfo, blocksInfo, nil
		case rd > 0:
			// Update block hashes
			block = IndexBlock(buf[0:rd])
			block.Position = blockNum
			blocksInfo = append(blocksInfo, block)

			// update file hash
			sha1.Write(buf[0:rd])

			// Increment block counter
			blockNum++
		}
	}
	panic("Impossible")
}
开发者ID:cmars,项目名称:replican-sync,代码行数:51,代码来源:index.go


示例8: MojangSha1Hex

func MojangSha1Hex(arrBytes ...[]byte) (val string) {
	sha1 := sha1.New()
	for _, bytes := range arrBytes {
		sha1.Write(bytes)
	}
	hash := sha1.Sum(nil)
	negative := (hash[0] & 0x80) == 0x80
	if negative {
		twosCompliment(hash)
	}
	val = hex.EncodeToString(hash)
	val = strings.TrimLeft(val, "0")
	if negative {
		val = "-" + val
	}
	return
}
开发者ID:LivousCraftNetwork,项目名称:GoLilyPad,代码行数:17,代码来源:util.go


示例9: MojangSha1Hex

func MojangSha1Hex(arrBytes ...[]byte) string {
	sha1 := sha1.New()
	for _, bytes := range arrBytes {
		sha1.Write(bytes)
	}
	hash := sha1.Sum(nil)
	negative := (hash[0] & 0x80) == 0x80
	if negative {
		twosCompliment(hash)
	}
	hexString := hex.EncodeToString(hash)
	hexString = strings.TrimLeft(hexString, "0")
	if negative {
		hexString = "-" + hexString
	}
	return hexString
}
开发者ID:Jumla,项目名称:GoLilyPad,代码行数:17,代码来源:util.go


示例10: calcFileInfo

func calcFileInfo(fi *FileInfo) {
	fmt.Printf("calcFileInfo: '%s'\n", fi.Path)
	var buf [16 * 1024]byte
	r, err := os.Open(fi.Path)
	fataliferr(err)
	defer r.Close()
	sha1 := sha1.New()
	md5Hash := md5.New()
	fi.Size = 0
	for {
		n, err := r.Read(buf[:])
		if err == io.EOF {
			break
		}
		d := buf[:n]
		fataliferr(err)
		fatalif(n == 0, "n is 0")
		fi.Size += int64(n)
		_, err = sha1.Write(d)
		fataliferr(err)
		_, err = md5Hash.Write(d)
		fataliferr(err)
	}
	sha1Sum := sha1.Sum(nil)
	fi.Sha1Hex = fmt.Sprintf("%x", sha1Sum)

	md5Sum := md5Hash.Sum(nil)
	fi.Md5Hex = fmt.Sprintf("%x", md5Sum)
	fi.Md5B64 = base64.StdEncoding.EncodeToString(md5Sum)

	fi.S3PathSha1Part = sha1HexToS3Path(fi.Sha1Hex)
	ext := strings.ToLower(filepath.Ext(fi.Path))
	fi.S3FullPath = s3Dir + fi.S3PathSha1Part + ext
	fmt.Printf("  sha1: %s\n", fi.Sha1Hex)
	fmt.Printf("   md5: %s\n", fi.Md5Hex)
	fmt.Printf("   md5: %s\n", fi.Md5B64)
	fmt.Printf("    s3: %s\n", fi.S3FullPath)
	fmt.Printf("  size: %d\n", fi.Size)
}
开发者ID:cnhans,项目名称:sumatrapdf,代码行数:39,代码来源:main.go


示例11: getClientProof

func getClientProof(user string, password string, salt []byte, keyA *big.Int, keyB *big.Int, keya *big.Int) (keyM []byte, keyK []byte) {
	// M = H(H(N) xor H(g), H(I), s, A, B, K)
	prime, g, _ := getPrime()
	keyK = getClientSession(user, password, salt, keyA, keyB, keya)

	n1 := bytesToBig(bigToSha1(prime))
	n2 := bytesToBig(bigToSha1(g))
	n3 := mathutil.ModPowBigInt(n1, n2, prime)
	n4 := getStringHash(user)
	sha1 := sha1.New()
	sha1.Write(n3.Bytes())
	sha1.Write(n4.Bytes())
	sha1.Write(salt)
	sha1.Write(keyA.Bytes())
	sha1.Write(keyB.Bytes())
	sha1.Write(keyK)
	keyM = sha1.Sum(nil)

	return keyM, keyK
}
开发者ID:kleopatra999,项目名称:firebirdsql,代码行数:20,代码来源:srp.go


示例12: Sha1Hex

func Sha1Hex(str string) (val string) {
	sha1 := sha1.New()
	sha1.Write([]byte(str))
	val = hex.EncodeToString(sha1.Sum(nil))
	return
}
开发者ID:0xNiklas,项目名称:GoLilyPad,代码行数:6,代码来源:util.go


示例13: main

func main() {
	flag.Parse()
	sha1 := sha1.New()
	sha1.Write([]byte(flag.Arg(0)))
	fmt.Printf("%x\n", sha1.Sum(nil))
}
开发者ID:cesarkuroiwa,项目名称:golang,代码行数:6,代码来源:sha1.go


示例14: getSha1Str

func getSha1Str(reposig string) string {
	sha1 := sha1.New()
	sha1.Write([]byte(reposig))
	return fmt.Sprintf("%x", sha1.Sum(nil))
}
开发者ID:holygeek,项目名称:rid,代码行数:5,代码来源:rid.go


示例15: bigToSha1

func bigToSha1(n *big.Int) []byte {
	sha1 := sha1.New()
	sha1.Write(n.Bytes())

	return sha1.Sum(nil)
}
开发者ID:kleopatra999,项目名称:firebirdsql,代码行数:6,代码来源:srp.go


示例16: GetHash

func GetHash(blob *[]byte) string {
	sha1 := sha1.New()
	sha1.Write(*blob)
	return fmt.Sprintf("sha1-%x", sha1.Sum())
}
开发者ID:srijak,项目名称:blobstore,代码行数:5,代码来源:utils.go


示例17: Sha1Hex

func Sha1Hex(str string) string {
	sha1 := sha1.New()
	sha1.Write([]byte(str))
	return hex.EncodeToString(sha1.Sum(nil))
}
开发者ID:Jumla,项目名称:GoLilyPad,代码行数:5,代码来源:util.go


示例18: isValid

// isValid validates the checksum of a block.
func (b block) isValid() bool {
	sha1 := sha1.New()
	sha1.Write(b.Data[:b.Hdr.Size])
	return bytes.Equal(b.Hdr.Score[:], sha1.Sum(nil))
}
开发者ID:nickstenning,项目名称:doozerd,代码行数:6,代码来源:block.go


示例19: StrongChecksum

// Strong checksum algorithm used throughout replican
// For now, it's SHA-1.
func StrongChecksum(buf []byte) string {
	var sha1 = sha1.New()
	sha1.Write(buf)
	return toHexString(sha1)
}
开发者ID:vanackere,项目名称:replican-sync,代码行数:7,代码来源:index.go


示例20: calcFileInfo

func calcFileInfo(fi *FileInfo) {
	fmt.Printf("calcFileInfo: '%s'\n", fi.Path)
	const BufSize = 16 * 1024
	var buf [BufSize]byte
	r, err := os.Open(fi.Path)
	fataliferr(err)
	defer r.Close()
	sha1 := sha1.New()
	md5Hash := md5.New()
	fi.ShouldCompress = false
	tryCompressFirsBlock := shouldTryCompressFile(fi.Path)
	var gzw *gzip.Writer
	compressedData := &bytes.Buffer{}
	fi.Size = 0
	fi.CompressedData = nil
	for {
		n, err := r.Read(buf[:])
		if err == io.EOF {
			break
		}
		d := buf[:n]
		fataliferr(err)
		fatalif(n == 0, "n is 0")
		fi.Size += n
		_, err = sha1.Write(d)
		fataliferr(err)
		_, err = md5Hash.Write(d)
		fataliferr(err)
		if tryCompressFirsBlock {
			tryCompressFirsBlock = false
			gz, err := gzip.NewWriterLevel(compressedData, gzip.BestCompression)
			fataliferr(err)
			_, err = gz.Write(d)
			fataliferr(err)
			gz.Close()
			compressedSize := compressedData.Len()
			saved := n - compressedSize
			// relatively high threshold of 20% savings on compression
			fi.ShouldCompress = saved > 0 && perc(compressedSize, saved) > 20
			diff := n - compressedSize
			fmt.Printf("  should compress: %v, %d => %d (%d %.2f%%)\n", fi.ShouldCompress, n, compressedSize, diff, perc(n, diff))
			if fi.ShouldCompress {
				compressedData = &bytes.Buffer{}
				gzw, err = gzip.NewWriterLevel(compressedData, gzip.BestCompression)
				fataliferr(err)
			}
		}
		if gzw != nil {
			_, err = gzw.Write(d)
			fataliferr(err)
		}
	}
	sha1Sum := sha1.Sum(nil)
	fi.Sha1Hex = fmt.Sprintf("%x", sha1Sum)

	if gzw != nil {
		gzw.Close()
		compressedSize := compressedData.Len()
		// only use compressed if compressed by at least 5%
		if compressedSize+(compressedSize/20) < fi.Size {
			fi.CompressedData = compressedData.Bytes()
		}
	}

	md5Sum := md5Hash.Sum(nil)
	fi.Md5Hex = fmt.Sprintf("%x", md5Sum)
	// if compressed, md5 is of the compressed content
	if fi.CompressedData != nil {
		md5Sum2 := md5.Sum(fi.CompressedData)
		fi.Md5Hex = fmt.Sprintf("%x", md5Sum2[:])
	}

	fi.S3PathSha1Part = sha1HexToS3Path(fi.Sha1Hex)
	ext := strings.ToLower(filepath.Ext(fi.Path))
	if fi.CompressedData != nil {
		fi.S3FullPath = fi.S3PathSha1Part + ".gz" + ext
	} else {
		fi.S3FullPath = fi.S3PathSha1Part + ext
	}
	fmt.Printf("  sha1: %s\n", fi.Sha1Hex)
	fmt.Printf("   md5: %s\n", fi.Md5Hex)
	fmt.Printf("    s3: %s\n", fi.S3FullPath)
	fmt.Printf("  size: %d\n", fi.Size)
	if fi.CompressedData != nil {
		sizedCompressed := len(fi.CompressedData)
		saved := fi.Size - sizedCompressed
		fmt.Printf("  size compressed: %d (saves %d %.2f%%)\n", sizedCompressed, saved, perc(fi.Size, saved))
	}
}
开发者ID:vipontes,项目名称:sumatrapdf,代码行数:89,代码来源:main.go



注:本文中的crypto/sha1.Write函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sha256.New函数代码示例发布时间:2022-05-24
下一篇:
Golang sha1.Sum函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap