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

Golang cipher.Block类代码示例

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

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



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

示例1: Encrypt

// Encrypts the contents of a file and returns the encrypted byte slice.
// AES encryption with PCKS#7 padding.
func Encrypt(filename, key string) (encrypted []byte, err error) {
	var (
		aesCipher cipher.Block
		data      []byte
		tempenc   = make([]byte, blockSize)
	)
	aesCipher, err = aes.NewCipher([]byte(key))
	if err != nil {
		return
	}

	data, err = ioutil.ReadFile(filename)
	if err != nil {
		return encrypted, err
	}

	padded, err := padding.Pad(data, blockSize)
	if err != nil {
		return encrypted, err
	}
	for i := 0; i < len(padded)/blockSize; i++ {
		aesCipher.Encrypt(tempenc, padded[i*blockSize:i*blockSize+blockSize])
		encrypted = append(encrypted, tempenc...)
	}
	return encrypted, nil
}
开发者ID:Wombats,项目名称:wombat-desktop,代码行数:28,代码来源:encryption.go


示例2: 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


示例3: newCBC

func newCBC(b cipher.Block, IV []byte) *cbc {
	return &cbc{
		b:         b,
		blockSize: b.BlockSize(),
		IV:        IV,
	}
}
开发者ID:CuriousLLC,项目名称:ecbcipher,代码行数:7,代码来源:ecb.go


示例4: newECB

func newECB(b cipher.Block) *ecb {

	return &ecb{
		b:         b,
		blockSize: b.BlockSize(),
	}
}
开发者ID:weixinhost,项目名称:beego-yar,代码行数:7,代码来源:aes.go


示例5: 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


示例6: 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


示例7: decrypt

func decrypt(src io.Reader, des io.Writer, cipher cipher.Block, length int) error {
	blockSize := cipher.BlockSize()
	if length <= 0 {
		return errors.New("length must be greater than 0")
	}
	buf := make([]byte, blockSize)
	acc := 0

	n, err := src.Read(buf)
	for err == nil && acc < length {
		acc += n
		cipher.Decrypt(buf, buf)

		if acc > length {
			paddings := acc - length
			des.Write(buf[0 : blockSize-paddings])
			return nil
		}
		des.Write(buf)
		buf = make([]byte, blockSize)

		n, err = src.Read(buf)
	}
	return nil
}
开发者ID:crayontxx,项目名称:go-rc5,代码行数:25,代码来源:cipher_ecb.go


示例8: 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


示例9: 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


示例10: 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


示例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: aesTransform

// aesTransform - encrypt or decrypt (according to "direction") using block
// cipher "bc" (typically AES)
func aesTransform(dst []byte, src []byte, direction directionConst, bc cipher.Block) {
	if direction == DirectionEncrypt {
		bc.Encrypt(dst, src)
		return
	} else if direction == DirectionDecrypt {
		bc.Decrypt(dst, src)
		return
	}
}
开发者ID:ncw,项目名称:rclone,代码行数:11,代码来源:eme.go


示例13: 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


示例14: get_key

func get_key(master cipher.Block, ctr uint64) (cipher.Block, []byte) {
	plain := make([]byte, 16)
	new_key := make([]byte, 16)
	binary.BigEndian.PutUint64(plain, ctr)
	master.Encrypt(new_key, plain)
	new_cipher, err := aes.NewCipher(new_key)
	check(err)
	return new_cipher, new_key
}
开发者ID:arthaud,项目名称:write-ups-2016,代码行数:9,代码来源:prover.go


示例15: updateMAC

// updateMAC reseeds the given hash with encrypted seed.
// it returns the first 16 bytes of the hash sum after seeding.
func updateMAC(mac hash.Hash, block cipher.Block, seed []byte) []byte {
	aesbuf := make([]byte, aes.BlockSize)
	block.Encrypt(aesbuf, mac.Sum(nil))
	for i := range aesbuf {
		aesbuf[i] ^= seed[i]
	}
	mac.Write(aesbuf)
	return mac.Sum(nil)[:16]
}
开发者ID:ruflin,项目名称:go-ethereum,代码行数:11,代码来源:rlpx.go


示例16: Encrypt

// Encrypt encrypts the plaintext src and outputs the corresponding ciphertext into dst.
// Besides outputting a ciphertext into dst, Encrypt also outputs an authentication tag
// of ocb2.TagSize bytes into tag, which should be used to verify the authenticity of the
// message on the receiving side.
//
// To ensure both authenticity and secrecy of messages, each invocation to this function must
// be given an unique nonce of ocb2.NonceSize bytes.  The nonce need not be secret (it can be
// a counter), but it needs to be unique.
//
// The block cipher used in function must work on a block size equal to ocb2.BlockSize.
// The tag slice used in this function must have a length equal to ocb2.TagSize.
// The nonce slice used in this function must have a length equal to ocb2.NonceSize.
// If any of the above are violated, Encrypt will panic.
func Encrypt(cipher cipher.Block, dst []byte, src []byte, nonce []byte, tag []byte) {
	if cipher.BlockSize() != BlockSize {
		panic("ocb2: cipher blocksize is not equal to ocb2.BlockSize")
	}
	if len(nonce) != NonceSize {
		panic("ocb2: nonce length is not equal to ocb2.NonceSize")
	}

	var (
		checksum [BlockSize]byte
		delta    [BlockSize]byte
		tmp      [BlockSize]byte
		pad      [BlockSize]byte
		calcTag  [NonceSize]byte
		off      int
	)

	cipher.Encrypt(delta[0:], nonce[0:])
	zeros(checksum[0:])

	remain := len(src)
	for remain > BlockSize {
		times2(delta[0:])
		xor(tmp[0:], delta[0:], src[off:off+BlockSize])
		cipher.Encrypt(tmp[0:], tmp[0:])
		xor(dst[off:off+BlockSize], delta[0:], tmp[0:])
		xor(checksum[0:], checksum[0:], src[off:off+BlockSize])
		remain -= BlockSize
		off += BlockSize
	}

	times2(delta[0:])
	zeros(tmp[0:])
	num := remain * 8
	tmp[BlockSize-2] = uint8((uint32(num) >> 8) & 0xff)
	tmp[BlockSize-1] = uint8(num & 0xff)
	xor(tmp[0:], tmp[0:], delta[0:])
	cipher.Encrypt(pad[0:], tmp[0:])
	copied := copy(tmp[0:], src[off:])
	if copied != remain {
		panic("ocb2: copy failed")
	}
	if copy(tmp[copied:], pad[copied:]) != (BlockSize - remain) {
		panic("ocb2: copy failed")
	}
	xor(checksum[0:], checksum[0:], tmp[0:])
	xor(tmp[0:], pad[0:], tmp[0:])
	if copy(dst[off:], tmp[0:]) != remain {
		panic("ocb2: copy failed")
	}

	times3(delta[0:])
	xor(tmp[0:], delta[0:], checksum[0:])
	cipher.Encrypt(calcTag[0:], tmp[0:])
	copy(tag, calcTag[:])
}
开发者ID:carriercomm,项目名称:grumble,代码行数:69,代码来源:ocb2.go


示例17: aesDecrypt

func aesDecrypt(block cipher.Block, src []byte) []byte {
	var dst = make([]byte, 16)
	var src_len = len(src)
	var dec []byte
	for i := 0; i < src_len; i += 16 {
		block.Decrypt(dst, src[i:i+16])
		dec = append(dec, dst...)
	}
	return dec
}
开发者ID:qingtao,项目名称:mana,代码行数:10,代码来源:aes.go


示例18: 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


示例19: 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


示例20: 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



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang cipher.BlockMode类代码示例发布时间:2022-05-24
下一篇:
Golang cipher.AEAD类代码示例发布时间: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