golang中crypto/dsa包

栏目: Go · 发布时间: 5年前

内容简介:DSA是用于数字签名的一种算法,私钥进行签名,公钥进行验证。关于更多dsa内容以及dsa实现移步那么go语言中怎么进行数字签名呢?go语言中怎么进行数字验证呢?下面给出这两个操作的案例代码:

DSA是用于数字签名的一种算法,私钥进行签名,公钥进行验证。关于更多dsa内容以及dsa实现移步 www.baidu.com

那么 go 语言中怎么进行数字签名呢?go语言中怎么进行数字验证呢?下面给出这两个操作的案例代码:

package main

import (
    "crypto/dsa"
    "crypto/rand"
    "fmt"
)

func main() {
    // parameters 是私钥的参数
    var param dsa.Parameters
    // L1024N160是一个枚举,根据L1024N160来决定私钥的长度(L N)
    dsa.GenerateParameters(&param, rand.Reader, dsa.L1024N160)
    // 定义私钥的变量
    var privateKey dsa.PrivateKey
    // 设置私钥的参数
    privateKey.Parameters = param
    // 生成密钥对
    dsa.GenerateKey(&privateKey, rand.Reader)
    // 公钥是存在在私钥中的,从私钥中读取公钥
    publicKey := privateKey.PublicKey
    message := []byte("hello,dsa签名")

    // 进入签名操作
    r, s, _ := dsa.Sign(rand.Reader, &privateKey, message)

    // 进入验证
    flag := dsa.Verify(&publicKey, message, r, s)
    if flag {
        fmt.Println("数据未被修改")
    } else {
        fmt.Println("数据被修改")
    }
    flag = dsa.Verify(&publicKey, []byte("hello"), r, s)
    if flag {
        fmt.Println("数据未被修改")
    } else {
        fmt.Println("数据被修改")
    }
}
输出:
数据未被修改
数据被修改

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Numerical Methods and Methods of Approximation in Science and En

Numerical Methods and Methods of Approximation in Science and En

Karan Surana / CRC Press / 2018-10-31

ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具