当前位置: 首页>>代码示例>>Golang>>正文


Golang md5.New函数代码示例

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


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

示例1: TestSlice

func TestSlice(t *testing.T) {
	a := []string{"abc", "def"}
	h := md5.New()
	io.WriteString(h, a[0])
	io.WriteString(h, a[1])
	expected := h.Sum(nil)
	check(t, expected, a)

	b := []int{1, 2, 3}
	h = md5.New()
	binary.Write(h, binary.BigEndian, b[0])
	binary.Write(h, binary.BigEndian, b[1])
	binary.Write(h, binary.BigEndian, b[2])
	expected = h.Sum(nil)
	check(t, expected, b)

	p := []*int{}
	x, y, z := int(1), int(2), int(3)
	p = append(p, &x, &y, &z, nil)
	h = md5.New()
	binary.Write(h, binary.BigEndian, x)
	binary.Write(h, binary.BigEndian, y)
	binary.Write(h, binary.BigEndian, z)
	h.Write([]byte{0})
	expected = h.Sum(nil)
	check(t, expected, p)
}
开发者ID:t-yuki,项目名称:cups-connector,代码行数:27,代码来源:deephash_test.go

示例2: loginClassic

func (socket *mongoSocket) loginClassic(cred Credential) error {
	// Note that this only works properly because this function is
	// synchronous, which means the nonce won't get reset while we're
	// using it and any other login requests will block waiting for a
	// new nonce provided in the defer call below.
	nonce, err := socket.getNonce()
	if err != nil {
		return err
	}
	defer socket.resetNonce()

	psum := md5.New()
	psum.Write([]byte(cred.Username + ":mongo:" + cred.Password))

	ksum := md5.New()
	ksum.Write([]byte(nonce + cred.Username))
	ksum.Write([]byte(hex.EncodeToString(psum.Sum(nil))))

	key := hex.EncodeToString(ksum.Sum(nil))

	cmd := authCmd{Authenticate: 1, User: cred.Username, Nonce: nonce, Key: key}
	res := authResult{}
	return socket.loginRun(cred.Source, &cmd, &res, func() error {
		if !res.Ok {
			return errors.New(res.ErrMsg)
		}
		socket.Lock()
		socket.dropAuth(cred.Source)
		socket.creds = append(socket.creds, cred)
		socket.Unlock()
		return nil
	})
}
开发者ID:NotBlizzard,项目名称:bluebirdmini,代码行数:33,代码来源:auth.go

示例3: main

func main() {
	fmt.Printf("Login......\n")
	unixTime := getUnixTime()

	//*******************token process begin************************
	var orignalToken = getToken(unixTime)
	//fmt.Printf("%s \n",orignalToken)
	vm.Set("token", orignalToken)
	token, error := vm.Run(encyptFunctions)
	if error != nil {
		//handler error
	}
	md5_t := md5.New()
	s_token, error := token.ToString()
	if error != nil {
		//handler error
	}
	md5_t.Write([]byte(s_token))
	true_token := hex.EncodeToString(md5_t.Sum(nil))
	//*******************token process end************************

	time.Sleep(1 * time.Second)
	email := "" //your email
	pwd := ""   //your password
	md5_p := md5.New()
	md5_p.Write([]byte(pwd))
	passwd := hex.EncodeToString(md5_p.Sum(nil))

	result := login(true_token, unixTime, email, passwd)
	fmt.Printf("%s\n", result)
}
开发者ID:ZH3FENG,项目名称:BypassCaptcha,代码行数:31,代码来源:loginTest.go

示例4: testMultipartObjectCreation

func testMultipartObjectCreation(c *check.C, create func() Driver) {
	drivers := create()
	err := drivers.CreateBucket("bucket", "")
	c.Assert(err, check.IsNil)
	uploadID, err := drivers.NewMultipartUpload("bucket", "key", "")
	c.Assert(err, check.IsNil)

	parts := make(map[int]string)
	finalHasher := md5.New()
	for i := 1; i <= 10; i++ {
		randomPerm := rand.Perm(10)
		randomString := ""
		for _, num := range randomPerm {
			randomString = randomString + strconv.Itoa(num)
		}

		hasher := md5.New()
		finalHasher.Write([]byte(randomString))
		hasher.Write([]byte(randomString))
		expectedmd5Sum := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
		expectedmd5Sumhex := hex.EncodeToString(hasher.Sum(nil))

		calculatedmd5sum, err := drivers.CreateObjectPart("bucket", "key", uploadID, i, "", expectedmd5Sum, int64(len(randomString)),
			bytes.NewBufferString(randomString))
		c.Assert(err, check.IsNil)
		c.Assert(calculatedmd5sum, check.Equals, expectedmd5Sumhex)
		parts[i] = calculatedmd5sum
	}
	finalExpectedmd5SumHex := hex.EncodeToString(finalHasher.Sum(nil))
	calculatedFinalmd5Sum, err := drivers.CompleteMultipartUpload("bucket", "key", uploadID, parts)
	c.Assert(err, check.IsNil)
	c.Assert(calculatedFinalmd5Sum, check.Equals, finalExpectedmd5SumHex)
}
开发者ID:tjyang,项目名称:minio,代码行数:33,代码来源:api_testsuite.go

示例5: main

func main() {
	/*
		bytes1 := []byte{53, 54, 52, 50, 51, 50, 54, 212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126}
		fmt.Println(bytes1)
		fmt.Println(string(bytes1))

		bytes2 := []byte{25, 27, 244, 217, 128, 26, 104, 209, 142, 206, 225, 61, 224, 35, 194, 25, 34, 163, 48, 192, 204, 109, 175, 139, 249, 236, 223, 153, 231, 28, 80, 164}
		fmt.Println(bytes2)
		fmt.Sprintf("%s", bytes2)
	*/

	h := md5.New()
	c := h.Sum([]byte("test"))
	fmt.Sprintf("%x", []byte("test"))
	fmt.Println([]byte("test"))
	fmt.Println(string([]byte("test")))
	fmt.Println(fmt.Sprintf("%x", c))
	fmt.Println(c)
	fmt.Println(string(c))

	h1 := md5.New()
	io.WriteString(h1, "test")
	c2 := h1.Sum(nil)
	fmt.Sprintf("%x", c2)
	// fmt.Println(string(c2))

	bytes := []byte{53, 53, 53, 53}
	c1 := h.Sum(bytes)
	fmt.Sprintf("%x", c1)
	// fmt.Println(c1)

	// writeResult(c)
}
开发者ID:zhouzhefu,项目名称:go-study,代码行数:33,代码来源:byte_array.go

示例6: TestSendMessageWithAttributes

func (s *S) TestSendMessageWithAttributes(c *C) {
	testServer.PrepareResponse(200, nil, TestSendMessageXmlOK)

	q := &Queue{s.sqs, testServer.URL + "/123456789012/testQueue/"}
	attrs := map[string]string{
		"test_attribute_name_1": "test_attribute_value_1",
	}
	resp, err := q.SendMessageWithAttributes("This is a test message", attrs)
	req := testServer.WaitRequest()

	c.Assert(req.Method, Equals, "GET")
	c.Assert(req.URL.Path, Equals, "/123456789012/testQueue/")
	c.Assert(req.Header["Date"], Not(Equals), "")

	var attrsHash = md5.New()
	attrsHash.Write(encodeMessageAttribute("test_attribute_name_1"))
	attrsHash.Write(encodeMessageAttribute("String"))
	attrsHash.Write([]byte{1})
	attrsHash.Write(encodeMessageAttribute("test_attribute_value_1"))
	c.Assert(resp.MD5OfMessageAttributes, Equals, fmt.Sprintf("%x", attrsHash.Sum(nil)))

	msg := "This is a test message"
	var h hash.Hash = md5.New()
	h.Write([]byte(msg))
	c.Assert(resp.MD5, Equals, fmt.Sprintf("%x", h.Sum(nil)))
	c.Assert(resp.Id, Equals, "5fea7756-0ea4-451a-a703-a558b933e274")
	c.Assert(err, IsNil)
}
开发者ID:DataDog,项目名称:goamz,代码行数:28,代码来源:sqs_test.go

示例7: TestCollectionReaderManyBlocks

func (s *CollectionReaderUnit) TestCollectionReaderManyBlocks(c *check.C) {
	h := md5.New()
	buf := make([]byte, 4096)
	locs := make([]string, len(buf))
	filesize := 0
	for i := 0; i < len(locs); i++ {
		_, err := io.ReadFull(rand.Reader, buf[:i])
		c.Assert(err, check.IsNil)
		h.Write(buf[:i])
		locs[i], _, err = s.kc.PutB(buf[:i])
		c.Assert(err, check.IsNil)
		filesize += i
	}
	manifest := "./random " + strings.Join(locs, " ") + " 0:" + strconv.Itoa(filesize) + ":bytes.bin\n"
	dataMD5 := h.Sum(nil)

	checkMD5 := md5.New()
	rdr, err := s.kc.CollectionFileReader(map[string]interface{}{"manifest_text": manifest}, "random/bytes.bin")
	c.Check(err, check.IsNil)
	_, err = io.Copy(checkMD5, rdr)
	c.Check(err, check.IsNil)
	_, err = rdr.Read(make([]byte, 1))
	c.Check(err, check.Equals, io.EOF)
	c.Check(checkMD5.Sum(nil), check.DeepEquals, dataMD5)
}
开发者ID:Kunde21,项目名称:arvados,代码行数:25,代码来源:collectionreader_test.go

示例8: TestRead

func (h *HashcheckSuiteSuite) TestRead(c *C) {
	hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))

	{
		r, w := io.Pipe()
		hcr := HashCheckingReader{r, md5.New(), hash}
		go func() {
			w.Write([]byte("foo"))
			w.Close()
		}()
		p, err := ioutil.ReadAll(hcr)
		c.Check(len(p), Equals, 3)
		c.Check(err, Equals, nil)
	}

	{
		r, w := io.Pipe()
		hcr := HashCheckingReader{r, md5.New(), hash}
		go func() {
			w.Write([]byte("bar"))
			w.Close()
		}()
		p, err := ioutil.ReadAll(hcr)
		c.Check(len(p), Equals, 3)
		c.Check(err, Equals, BadChecksum)
	}
}
开发者ID:ntijanic,项目名称:arvados,代码行数:27,代码来源:hashcheck_test.go

示例9: login

func login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm() //解析参数
	Println("method:", r.Method)
	h := md5.New()
	test := md5.New()
	// str := "hebihui"
	// test.Write([]byte(str))
	haha := Sprintf("%x", test.Sum(nil))
	Println("my:", haha)
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := Sprintf("%x", h.Sum(nil))
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, token)
	} else {
		token := r.Form.Get("token")
		if token != "" {
			if isValidation(token) {
				table[token] = 1
				Fprintf(w, "OK.")
			} else {
				Fprintf(w, "fail.")
			}
		} else {
			Fprintf(w, "null") //不合法
		}
		Println("username:", r.Form["username"])
		Println("password:", r.Form["password"])
	}
}
开发者ID:hebihui,项目名称:go_study,代码行数:31,代码来源:login.go

示例10: main

func main() {
	TestString := "Hi,panda!"

	Md5Inst := md5.New()
	Md5Inst.Write([]byte(TestString))
	Result := Md5Inst.Sum([]byte(""))
	fmt.Printf("%x\n\n", Result)

	Sha1Inst := sha1.New()
	Sha1Inst.Write([]byte(TestString))
	Result = Sha1Inst.Sum([]byte(""))
	fmt.Printf("%x\n\n", Result)

	TestFile := "123.txt"
	infile, inerr := os.Open(TestFile)
	if inerr == nil {
		md5h := md5.New()
		io.Copy(md5h, infile)
		fmt.Printf("%x , %s \n", md5h.Sum([]byte("")), TestFile)

		sha1h := sha1.New()
		io.Copy(sha1h, infile)
		fmt.Printf("%x ,%s \n", sha1h.Sum([]byte("")), TestFile)
	} else {
		fmt.Println(inerr)
		os.Exit(1)
	}
}
开发者ID:smilefish1987,项目名称:funshion,代码行数:28,代码来源:hashFunc.go

示例11: Insert

/* 插入用户 */
func (this *Member) Insert() string {
	if this.Id == "" { //id没有设置则初始化新的
		this.Id = bson.NewObjectId()
	}
	this.LastTime = bson.Now()
	this.StopTime = bson.Now()
	this.SalayTime = bson.Now()
	this.Token = strconv.Itoa(int(rand.New(rand.NewSource(time.Now().UnixNano())).Uint32()))
	this.ErrorChance = 6
	//如果没有头像,随机设置一个头像
	if this.Image == "" {
		this.Image = strconv.Itoa(rand.Intn(9))
	}
	//密码md5加盐+用户ID+md5加密
	m := md5.New()
	m.Write([]byte(this.Password))
	n := md5.New()
	n.Write([]byte(hex.EncodeToString(m.Sum(nil)) + beego.AppConfig.String("md5salt") + bson.ObjectId.Hex(this.Id)))
	this.Password = hex.EncodeToString(n.Sum(nil))
	err := memberC.Insert(this)
	if err != nil {
		panic(err)
	}
	return bson.ObjectId.Hex(this.Id)
}
开发者ID:treejames,项目名称:woku_old,代码行数:26,代码来源:member.go

示例12: CheckPass

/* 校验密码是否正确 支持类型:邮箱 */
func (this *Member) CheckPass(account string, password string) (string, int) {
	err := memberC.Find(bson.M{"e": account}).One(&this)
	if err != nil {
		return "", -2 //账号不存在
	}

	if bson.Now().Before(this.StopTime) { //锁定时间还没过
		long := this.StopTime.Sub(bson.Now())
		return strconv.FormatFloat(long.Seconds(), 'f', 0, 64) + " 秒", -3
	}
	//密码加密
	m := md5.New()
	m.Write([]byte(password))
	n := md5.New()
	n.Write([]byte(hex.EncodeToString(m.Sum(nil)) + beego.AppConfig.String("md5salt") + bson.ObjectId.Hex(this.Id)))
	//对比
	if hex.EncodeToString(n.Sum(nil)) != this.Password { //验证出错
		if this.ErrorChance <= 1 { //用尽验证机会,账号锁定10分钟
			this.ErrorChance = 6
			minute := time.Duration(10) * time.Minute
			this.StopTime = bson.Now().Add(minute)
			this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance, "st": this.StopTime}})
			return "", -4 //进入锁定
		} else { //验证机会-1
			this.ErrorChance--
			this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance}})
			return strconv.Itoa(int(this.ErrorChance)), -1 //密码不匹配
		}
	} else { //通过验证,重置机会次数
		this.ErrorChance = 6
		this.Update(bson.M{"$set": bson.M{"er": this.ErrorChance}})
		return this.Id.Hex(), 1
	}
}
开发者ID:treejames,项目名称:woku_old,代码行数:35,代码来源:member.go

示例13: newPutter

// Sends an S3 multipart upload initiation request.
// See http://docs.amazonwebservices.com/AmazonS3/latest/dev/mpuoverview.html.
// The initial request returns an UploadId that we use to identify
// subsequent PUT requests.
func newPutter(url url.URL, h http.Header, c *Config, b *Bucket) (p *putter, err error) {
	p = new(putter)
	p.url = url
	p.c, p.b = new(Config), new(Bucket)
	*p.c, *p.b = *c, *b
	p.c.Concurrency = max(c.Concurrency, 1)
	p.c.NTry = max(c.NTry, 1)
	p.bufsz = max64(minPartSize, c.PartSize)
	resp, err := p.retryRequest("POST", url.String()+"?uploads", nil, h)
	if err != nil {
		return nil, err
	}
	defer checkClose(resp.Body, err)
	if resp.StatusCode != 200 {
		return nil, newRespError(resp)
	}
	err = xml.NewDecoder(resp.Body).Decode(p)
	if err != nil {
		return nil, err
	}
	p.ch = make(chan *part)
	for i := 0; i < p.c.Concurrency; i++ {
		go p.worker()
	}
	p.md5OfParts = md5.New()
	p.md5 = md5.New()

	p.sp = bufferPool(p.bufsz)

	return p, nil
}
开发者ID:reem,项目名称:s3gof3r,代码行数:35,代码来源:putter.go

示例14: main

func main() {
	// md5
	h := md5.New()
	h.Write([]byte("404 Pages"))
	//fmt.Printf("%x \n", h.Sum(nil))
	s := "0B2441996dc4ebeb1a356ebfe1b3e9a5"

	fmt.Println(fmt.Sprintf("%x", h.Sum(nil)))

	if strings.EqualFold(s, fmt.Sprintf("%x", h.Sum(nil))) {
		fmt.Println("They are equals! EqualFold")
	}

	if s == fmt.Sprintf("%x", h.Sum(nil)) {
		fmt.Println("They are equals!")
	}

	// sha-1
	h = sha1.New()
	h.Write([]byte("404 Pages"))
	fmt.Printf("%x \n", h.Sum(nil))

	// 文件hash
	f := md5.New()
	file := "F:\\cmdlet.txt"
	r, err := os.Open(file)
	if err != nil {
		return
	}
	defer r.Close()
	b, _ := ioutil.ReadAll(r)
	f.Write(b)
	fmt.Printf("File MD5: %x \n", f.Sum(nil))
}
开发者ID:hyndio,项目名称:hyd.me,代码行数:34,代码来源:diff.go

示例15: TestInt

func TestInt(t *testing.T) {
	i := int(123456789)
	h := md5.New()
	binary.Write(h, binary.BigEndian, i)
	expected := h.Sum(nil)
	check(t, expected, i)

	i8 := int8(123)
	h = md5.New()
	binary.Write(h, binary.BigEndian, i8)
	expected = h.Sum(nil)
	check(t, expected, i8)

	// byte is an alias for uint8
	b := byte('Q')
	h = md5.New()
	binary.Write(h, binary.BigEndian, b)
	expected = h.Sum(nil)
	check(t, expected, b)

	// rune is an alias for int32
	r := rune('龍')
	h = md5.New()
	binary.Write(h, binary.BigEndian, r)
	expected = h.Sum(nil)
	check(t, expected, r)

	ui64 := uint64(123456789123456789)
	h = md5.New()
	binary.Write(h, binary.BigEndian, ui64)
	expected = h.Sum(nil)
	check(t, expected, ui64)
}
开发者ID:t-yuki,项目名称:cups-connector,代码行数:33,代码来源:deephash_test.go


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