首页 技术 正文
技术 2022年11月17日
0 收藏 894 点赞 2,760 浏览 1272 个字

前面说过要找时间介绍一下bccsp包下面的工厂factory,so here it is.

通过factory目前可以获得两类BCCSP实例,一个是上文说的sw,还有一个是通过pkcs11实现的。

BCCSP实例是通过工厂来提供的,sw包对应的工厂在swFactory.go中实现,pkcs11包对应的工厂在pkcs11Factory.go中实现,它们都共同实现了下面的BCCSPFactory接口:

// BCCSPFactory is used to get instances of the BCCSP interface.
// A Factory has name used to address it.
type BCCSPFactory interface { // Name returns the name of this factory
Name() string //用来返回bccsp接口实例名 // Get returns an instance of BCCSP using opts.
Get(opts *FactoryOpts) (bccsp.BCCSP, error) //根据opts配置返回具体的bccsp实例
}

需要注意的是,在factory.go源码中,用全局变量bccspMap(一个BCCSP的数组)来保存这些实例化的bccsp,同时用一个全局变量bootBCCSP来保存缺省的BCCSP接口实例.  这两个全局变量的初始化工作是通过调用InitFactories来实现的。

// InitFactories must be called before using factory interfaces
// It is acceptable to call with config = nil, in which case
// some defaults will get used
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
setFactories(config)
}) return factoriesInitError
}

1、在使用BCCSP之前都需要调用上面的函数来对bootBCCSP和bccspMap来赋值,之后就可以使用GetDefault()来获取缺省bootBCCSP(默认是sw)或者通过GetBCCSP(name string)来获取bccspMap中其他的BCCSP实例(sw或者pkcs11).  其实bccsp/factory包下面还提供了其他的方法来获取BCCSP实例,如也可以直接通过GetBCCSPFromOpts根据opts的设置来直接得到具体的BCCSP实例,这部分可以直接去看代码实现。

2、接下来就可以通过获取的BCCSP实例(如sw或者pkcs11实现)进行加解密,签名验证,哈希,密钥导入和派生以及密钥生成操作

——————————————

先把流程弄清楚然后撸代码,这样会爽一点。

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905