Ejemplo n.º 1
0
func (t *SignerTest) TestStringSigner(c *C) {
	s := signer.NewBase64Signer(h)

	table := []struct {
		msg []byte
		b   []byte
	}{
		{
			msg: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
			b:   []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x7a, 0x5f, 0x36, 0x74, 0x5f, 0x59, 0x58, 0x54, 0x2d, 0x4b, 0x5f, 0x4c, 0x6b, 0x69, 0x38, 0x77, 0x68, 0x4d, 0x65, 0x62, 0x39, 0x75, 0x43, 0x70, 0x42, 0x37, 0x6f},
		},
		{
			msg: []byte{},
			b:   []byte{0x2e, 0x6d, 0x72, 0x49, 0x56, 0x70, 0x56, 0x6b, 0x34, 0x55, 0x65, 0x41, 0x63, 0x70, 0x61, 0x49, 0x61, 0x51, 0x51, 0x34, 0x32, 0x38, 0x75, 0x36, 0x6a, 0x52, 0x57, 0x63},
		},
	}
	for _, r := range table {
		for i := 0; i < 10; i++ {
			c.Assert(s.Sign(r.msg), DeepEquals, r.b)
			msg, ok := s.Verify(r.b)
			c.Assert(msg, HasLen, len(r.msg))
			if len(msg) > 0 {
				c.Assert(msg, DeepEquals, r.msg)
			}
			c.Assert(ok, Equals, true)
		}
	}
}
Ejemplo n.º 2
0
func (t *SignerTest) BenchmarkBase64Sign(c *C) {
	s := signer.NewBase64Signer(h)
	data := []byte("hello")

	for i := 0; i < c.N; i++ {
		s.Sign(data)
	}
}
Ejemplo n.º 3
0
func (t *SignerTest) TestItsDangerousSignerCompatibility(c *C) {
	sha1Hash := sha1.New()
	sha1Hash.Write([]byte("itsdangerous.Signersignerfoo-bar"))
	key := sha1Hash.Sum(nil)
	h = hmac.New(func() hash.Hash {
		return sha1.New()
	}, key)

	s := signer.NewBase64Signer(h)
	msg := s.Sign([]byte("hello"))
	c.Assert(string(msg), Equals, "hello.z_6t_YXT-K_Lki8whMeb9uCpB7o")
}
Ejemplo n.º 4
0
func ExampleSign() {
	h := hmac.New(func() hash.Hash {
		return md5.New()
	}, []byte("secret"))
	s := signer.NewBase64Signer(h)

	msg := []byte("hello")
	b := s.Sign(msg)

	fmt.Println(string(b))
	// Output: hello.ut5jhjxh7QsxZYBuzWrO_A
}
Ejemplo n.º 5
0
func ExampleVerify() {
	h := hmac.New(func() hash.Hash {
		return md5.New()
	}, []byte("secret"))
	s := signer.NewBase64Signer(h)

	b := []byte("hello.ut5jhjxh7QsxZYBuzWrO_A")
	msg, ok := s.Verify(b)

	fmt.Println(ok)
	fmt.Println(string(msg))
	// Output:
	// true
	// hello
}