當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Block.BlockSize方法代碼示例

本文整理匯總了Golang中crypto/cipher.Block.BlockSize方法的典型用法代碼示例。如果您正苦於以下問題:Golang Block.BlockSize方法的具體用法?Golang Block.BlockSize怎麽用?Golang Block.BlockSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在crypto/cipher.Block的用法示例。


在下文中一共展示了Block.BlockSize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: newCBC

func newCBC(b cipher.Block, IV []byte) *cbc {
	return &cbc{
		b:         b,
		blockSize: b.BlockSize(),
		IV:        IV,
	}
}
開發者ID:CuriousLLC,項目名稱:ecbcipher,代碼行數:7,代碼來源:ecb.go

示例2: encode

// encode uses the given block cipher (in CTR mode) to encrypt the
// data, along with a hash, returning the iv and the ciphertext. What
// is returned looks like:
//
//   encrypted(salt + sessionData) + iv + hmac
//
func encode(block cipher.Block, hmac hash.Hash, data []byte) ([]byte, error) {

	buf := bytes.NewBuffer(nil)

	salt := make([]byte, block.BlockSize())
	if _, err := io.ReadFull(rand.Reader, salt); err != nil {
		return nil, err
	}
	buf.Write(salt)
	buf.Write(data)

	session := buf.Bytes()

	iv := make([]byte, block.BlockSize())
	if _, err := rand.Read(iv); err != nil {
		return nil, err
	}

	stream := cipher.NewCTR(block, iv)
	stream.XORKeyStream(session, session)

	buf.Write(iv)
	hmac.Write(buf.Bytes())
	buf.Write(hmac.Sum(nil))

	return buf.Bytes(), nil
}
開發者ID:beatgammit,項目名稱:seshcookie,代碼行數:33,代碼來源:seshcookie.go

示例3: newECB

func newECB(b cipher.Block) *ecb {

	return &ecb{
		b:         b,
		blockSize: b.BlockSize(),
	}
}
開發者ID:weixinhost,項目名稱:beego-yar,代碼行數:7,代碼來源:aes.go

示例4: encrypt

// encrypt encrypts a value using the given Block in CTR mode.
//
// A random initialization vector is generated and prepended to the resulting
// ciphertext to be available for decryption. Also, a random salt with the
// length of the block size is prepended to the value before encryption.
func encrypt(block cipher.Block, value []byte) (rv []byte, err error) {
	// Recover in case block has an invalid key.
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	size := block.BlockSize()
	// Generate an initialization vector suitable for encryption.
	// http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29
	iv := make([]byte, size)
	if _, err = rand.Read(iv); err != nil {
		return
	}
	// Create a salt.
	salt := make([]byte, size)
	if _, err = rand.Read(salt); err != nil {
		return
	}
	value = append(salt, value...)
	// Encrypt it.
	stream := cipher.NewCTR(block, iv)
	stream.XORKeyStream(value, value)
	// Return iv + ciphertext.
	rv = append(iv, value...)
	return
}
開發者ID:scyth,項目名稱:gorilla-sessions-filestore,代碼行數:32,代碼來源:sessions.go

示例5: NewOCFBEncrypter

// NewOCFBEncrypter returns a cipher.Stream which encrypts data with OpenPGP's
// cipher feedback mode using the given cipher.Block, and an initial amount of
// ciphertext.  randData must be random bytes and be the same length as the
// cipher.Block's block size. Resync determines if the "resynchronization step"
// from RFC 4880, 13.9 step 7 is performed. Different parts of OpenPGP vary on
// this point.
func NewOCFBEncrypter(block cipher.Block, randData []byte, resync OCFBResyncOption) (cipher.Stream, []byte) {
	blockSize := block.BlockSize()
	if len(randData) != blockSize {
		return nil, nil
	}

	x := &ocfbEncrypter{
		b:       block,
		fre:     make([]byte, blockSize),
		outUsed: 0,
	}
	prefix := make([]byte, blockSize+2)

	block.Encrypt(x.fre, x.fre)
	for i := 0; i < blockSize; i++ {
		prefix[i] = randData[i] ^ x.fre[i]
	}

	block.Encrypt(x.fre, prefix[:blockSize])
	prefix[blockSize] = x.fre[0] ^ randData[blockSize-2]
	prefix[blockSize+1] = x.fre[1] ^ randData[blockSize-1]

	if resync {
		block.Encrypt(x.fre, prefix[2:])
	} else {
		x.fre[0] = prefix[blockSize]
		x.fre[1] = prefix[blockSize+1]
		x.outUsed = 2
	}
	return x, prefix
}
開發者ID:Christopheraburns,項目名稱:clive,代碼行數:37,代碼來源:ocfb.go

示例6: CTREncrypt

func CTREncrypt(block cipher.Block, nonce, dst, src []byte) {
	size := block.BlockSize()
	if len(nonce) != size {
		panic("size of IV not equal to block size")
	}
	if len(dst) == 0 || len(src) == 0 {
		return
	}
	// temp key
	key := make([]byte, size)
	// copy of nonce
	n := make([]byte, size)
	copy(n, nonce)
	counter := binary.LittleEndian.Uint64(n[8:])
	for i := 0; i < len(dst) && i < len(src); i += size {
		block.Encrypt(key, n)

		for j := 0; j < size && i+j < len(src); j++ {
			dst[i+j] = src[i+j] ^ key[j]
		}
		counter++
		binary.LittleEndian.PutUint64(n[8:], counter)
	}
	return
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:25,代碼來源:util.go

示例7: decrypt

// decrypt decrypts a value using the given Block in CTR mode.
//
// The value to be decrypted must have a length greater than the block size,
// because the initialization vector is expected to prepend it. Also, a salt
// with the length of the block size is expected to prepend the plain value.
func decrypt(block cipher.Block, value []byte) (b []byte, err error) {
	// Recover in case block has an invalid key.
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	size := block.BlockSize()
	if len(value) > size {
		// Extract iv.
		iv := value[:size]
		// Extract ciphertext.
		value = value[size:]
		// Decrypt it.
		stream := cipher.NewCTR(block, iv)
		stream.XORKeyStream(value, value)
		if len(value) > size {
			// Return value without the salt.
			b = value[size:]
			return
		}
	}
	err = ErrDecryption
	return
}
開發者ID:scyth,項目名稱:gorilla-sessions-filestore,代碼行數:30,代碼來源:sessions.go

示例8: TestCipher

func TestCipher(t *testing.T) {
	var aes cipher.Block
	var err error
	for cipher_id, expected := range cipher_results {
		aes, err = NewCipher(cipher_id, key, nil)
		if err != nil {
			t.Fatal(err)
		}
		blocksize := aes.BlockSize()
		ciphertext := make([]byte, blocksize)
		aes.Encrypt(ciphertext, cleartext)
		deciphertext := make([]byte, blocksize)
		if !strings.EqualFold(expected, fmt.Sprintf("%x", ciphertext)) {
			t.Fatal("couldn't encrypt")
		}
		aes, err = NewCipher(cipher_id, key, nil)
		aes.Decrypt(deciphertext, ciphertext)
		if !bytes.Equal(cleartext, deciphertext) {
			t.Fatal("couldn't decrypt %s %s", cleartext, deciphertext)
		}
		if err != nil {
			t.Fatal(err)
		}
	}
}
開發者ID:postfix,項目名稱:gocryptodev,代碼行數:25,代碼來源:cipher_test.go

示例9: Encrypt

// Encrypt производит шифрования хранилища по паролю.
func Encrypt(s Storage, w io.Writer, pwd string) (err error) {
	var (
		key     [keySize]byte
		block   cipher.Block
		iv      []byte
		payload []byte
	)

	// Prepare encryptor
	key = sha256.Sum256([]byte(pwd))
	if block, err = aes.NewCipher(key[:]); err != nil {
		return err
	}
	iv = make([]byte, block.BlockSize())
	if _, err = rand.Read(iv); err != nil {
		return err
	}
	if _, err = w.Write(iv); err != nil {
		return err
	}
	w = cipher.StreamWriter{S: cipher.NewCFBEncrypter(block, iv), W: w}

	// Encode and write
	if _, err = w.Write(key[:]); err != nil {
		return err
	}
	if payload, err = json.Marshal(s); err != nil {
		return err
	}
	if _, err := w.Write(payload); err != nil {
		return err
	}
	return nil
}
開發者ID:yamnikov-oleg,項目名稱:pss,代碼行數:35,代碼來源:storage.go

示例10: generateSubkeys

// Given the supplied cipher, whose block size must be 16 bytes, return two
// subkeys that can be used in MAC generation. See section 5.3 of NIST SP
// 800-38B. Note that the other NIST-approved block size of 8 bytes is not
// supported by this function.
func generateSubkeys(ciph cipher.Block) (k1 []byte, k2 []byte) {
	if ciph.BlockSize() != blockSize {
		panic("generateSubkeys requires a cipher with a block size of 16 bytes.")
	}

	// Step 1
	l := make([]byte, blockSize)
	ciph.Encrypt(l, subkeyZero)

	// Step 2: Derive the first subkey.
	if common.Msb(l) == 0 {
		// TODO(jacobsa): Accept a destination buffer in ShiftLeft and then hoist
		// the allocation in the else branch below.
		k1 = common.ShiftLeft(l)
	} else {
		k1 = make([]byte, blockSize)
		common.Xor(k1, common.ShiftLeft(l), subkeyRb)
	}

	// Step 3: Derive the second subkey.
	if common.Msb(k1) == 0 {
		k2 = common.ShiftLeft(k1)
	} else {
		k2 = make([]byte, blockSize)
		common.Xor(k2, common.ShiftLeft(k1), subkeyRb)
	}

	return
}
開發者ID:itiserik,項目名稱:crypto,代碼行數:33,代碼來源:subkey.go

示例11: newRCBC

func newRCBC(b cipher.Block, iv []byte) *rcbc {
	return &rcbc{
		b:         b,
		blockSize: b.BlockSize(),
		iv:        dup(iv),
		tmp:       make([]byte, b.BlockSize()),
	}
}
開發者ID:optimuse,項目名稱:spig,代碼行數:8,代碼來源:rcbc.go

示例12: ECBEncrypt

func ECBEncrypt(block cipher.Block, dst, src []byte) {
	size := block.BlockSize()
	if len(dst)%size != 0 {
		panic("size of dst and src should be multiples of blocksize")
	}
	for i := 0; i < len(dst); i += size {
		block.Encrypt(dst[i:i+size], src[i:i+size])
	}
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:9,代碼來源:util.go

示例13: newCFB8

func newCFB8(block cipher.Block, iv []byte, decrypt bool) (stream cipher.Stream) {
	cfb8 := new(cfb8)
	cfb8.b = block
	cfb8.next = make([]byte, len(iv))
	cfb8.out = make([]byte, block.BlockSize())
	cfb8.decrypt = decrypt
	copy(cfb8.next, iv)
	stream = cfb8
	return
}
開發者ID:Jimdo,項目名稱:nsca,代碼行數:10,代碼來源:cfb8.go

示例14: newCFB8

func newCFB8(block cipher.Block, iv []byte, decrypt bool) (stream cipher.Stream) {
	bytes := make([]byte, len(iv))
	copy(bytes, iv)
	return &cfb8{
		block:   block,
		iv:      bytes,
		tmp:     make([]byte, block.BlockSize()),
		decrypt: decrypt,
	}
}
開發者ID:Jumla,項目名稱:GoLilyPad,代碼行數:10,代碼來源:cfb8.go

示例15: newCFB8

func newCFB8(block cipher.Block, iv []byte, decrypt bool) (stream cipher.Stream) {
	cfb8 := new(cfb8)
	cfb8.Block = block
	cfb8.Iv = make([]byte, len(iv))
	cfb8.Tmp = make([]byte, block.BlockSize())
	cfb8.Decrypt = decrypt
	copy(cfb8.Iv, iv)
	stream = cfb8
	return
}
開發者ID:LivousCraftNetwork,項目名稱:GoLilyPad,代碼行數:10,代碼來源:cfb8.go


注:本文中的crypto/cipher.Block.BlockSize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。