前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang语言情怀-第55期 Go 语言标准库翻译 crypto/aes

Golang语言情怀-第55期 Go 语言标准库翻译 crypto/aes

作者头像
李海彬
发布2021-03-09 11:02:21
2580
发布2021-03-09 11:02:21
举报
文章被收录于专栏:Golang语言社区Golang语言社区

import "crypto"

crypto包搜集了常用的密码(算法)常量。

代码语言:javascript
复制
type PublicKey
type PrivateKey
type Hash
func (h Hash) Available() bool
func (h Hash) Size() int
func (h Hash) New() hash.Hash
func RegisterHash(h Hash, f func() hash.Hash)

type PublicKey

代码语言:javascript
复制
type PublicKey interface{}

代表一个使用未指定算法的公钥。

type PrivateKey

代码语言:javascript
复制
type PrivateKey interface{}

代表一个使用未指定算法的私钥。

type Hash

代码语言:javascript
复制
type Hash uint

Hash用来识别/标识另一个包里实现的加密函数。

代码语言:javascript
复制
const (
    MD4       Hash = 1 + iota // 导入code.google.com/p/go.crypto/md4
    MD5                       // 导入crypto/md5
    SHA1                      // 导入crypto/sha1
    SHA224                    // 导入crypto/sha256
    SHA256                    // 导入crypto/sha256
    SHA384                    // 导入crypto/sha512
    SHA512                    // 导入crypto/sha512
    MD5SHA1                   // 未实现;MD5+SHA1用于TLS RSA
    RIPEMD160                 // 导入code.google.com/p/go.crypto/ripemd160
)
func (Hash) Available
代码语言:javascript
复制
func (h Hash) Available() bool

报告是否有hash函数注册到该标识值。

func (Hash) Size
代码语言:javascript
复制
func (h Hash) Size() int

返回给定hash函数返回值的字节长度。

func (Hash) New

代码语言:javascript
复制
func (h Hash) New() hash.Hash

创建一个使用给定hash函数的hash.Hash接口,如果该标识值未注册hash函数,将会panic。

func RegisterHash

代码语言:javascript
复制
func RegisterHash(h Hash, f func() hash.Hash)
注册一个返回给定hash接口实例的函数,并指定其标识值,该函数应在实现hash接口的包的init函数中调用。

import "crypto/aes"

aes包实现了AES加密算法,参见U.S. Federal Information Processing Standards Publication 197。

代码语言:javascript
复制
Constants
type KeySizeError
func (k KeySizeError) Error() string
func NewCipher(key []byte) (cipher.Block, error)

Constants

代码语言:javascript
复制
const BlockSize = 16

AES字节块大小。

type KeySizeError

代码语言:javascript
复制
type KeySizeError int
func (KeySizeError) Error
代码语言:javascript
复制
func (k KeySizeError) Error() string

func NewCipher

代码语言:javascript
复制
func NewCipher(key []byte) (cipher.Block, error)

创建一个cipher.Block接口。参数key为密钥,长度只能是16、24、32字节,用以选择AES-128、AES-192、AES-256。


参考资料:

Go语言中文文档

http://www.golang.ltd/

Go语言官方文档

https://golang.google.cn/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-02-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言情怀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • type PublicKey
  • type PrivateKey
  • type Hash
    • func (Hash) Available
      • func (Hash) Size
      • func RegisterHash
        • 注册一个返回给定hash接口实例的函数,并指定其标识值,该函数应在实现hash接口的包的init函数中调用。
        • Constants
        • type KeySizeError
          • func (KeySizeError) Error
          • func NewCipher
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档