本文整理汇总了Golang中crypto/sha1.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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))
}
示例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...")
}
示例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)
}
示例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
}
示例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()
}
示例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{}))
}
示例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")
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例12: Sha1Hex
func Sha1Hex(str string) (val string) {
sha1 := sha1.New()
sha1.Write([]byte(str))
val = hex.EncodeToString(sha1.Sum(nil))
return
}
示例13: main
func main() {
flag.Parse()
sha1 := sha1.New()
sha1.Write([]byte(flag.Arg(0)))
fmt.Printf("%x\n", sha1.Sum(nil))
}
示例14: getSha1Str
func getSha1Str(reposig string) string {
sha1 := sha1.New()
sha1.Write([]byte(reposig))
return fmt.Sprintf("%x", sha1.Sum(nil))
}
示例15: bigToSha1
func bigToSha1(n *big.Int) []byte {
sha1 := sha1.New()
sha1.Write(n.Bytes())
return sha1.Sum(nil)
}