github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/coder/dsa.go (about)

     1  package coder
     2  
     3  import (
     4  	"crypto/dsa"
     5  	"crypto/rand"
     6  	"encoding/asn1"
     7  	"encoding/pem"
     8  	"math/big"
     9  	"os"
    10  )
    11  
    12  func DSAGenerateKeyPair(size dsa.ParameterSizes, privateKeyPath string, publicKeyPath string) error {
    13  	var param dsa.Parameters
    14  	err := dsa.GenerateParameters(&param, rand.Reader, size)
    15  	if err != nil {
    16  		return err
    17  	}
    18  	var privKey dsa.PrivateKey
    19  	privKey.Parameters = param
    20  	err = dsa.GenerateKey(&privKey, rand.Reader)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	b, err := asn1.Marshal(privKey)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	privBlock := pem.Block{
    30  		Type:  "DSA Private Key",
    31  		Bytes: b,
    32  	}
    33  	privFile, err := os.Create(privateKeyPath)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	err = pem.Encode(privFile, &privBlock)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	pubKey := privKey.PublicKey
    43  	b, err = asn1.Marshal(pubKey)
    44  	pubBlock := pem.Block{
    45  		Type:  "DSA Public Key",
    46  		Bytes: b,
    47  	}
    48  	pubFile, err := os.Create(publicKeyPath)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	err = pem.Encode(pubFile, &pubBlock)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func DSASign(content string, privKeyPath string) (r, s *big.Int, err error) {
    60  	file, err := os.Open(privKeyPath)
    61  	if err != nil {
    62  		return nil, nil, err
    63  	}
    64  	defer func(file *os.File) { _ = file.Close() }(file)
    65  	info, _ := file.Stat()
    66  	buf := make([]byte, info.Size())
    67  	_, err = file.Read(buf)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	block, _ := pem.Decode(buf)
    72  	var privKey dsa.PrivateKey
    73  	_, err = asn1.Unmarshal(block.Bytes, &privKey)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  	rr, ss, err := dsa.Sign(rand.Reader, &privKey, []byte(content))
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  	return rr, ss, nil
    82  }
    83  
    84  func DSAVerify(content string, pubKeyPath string, r, s *big.Int) (bool, error) {
    85  	file, err := os.Open(pubKeyPath)
    86  	if err != nil {
    87  		return false, err
    88  	}
    89  	defer func(file *os.File) { _ = file.Close() }(file)
    90  	info, _ := file.Stat()
    91  	buf := make([]byte, info.Size())
    92  	_, err = file.Read(buf)
    93  	if err != nil {
    94  		return false, err
    95  	}
    96  	block, _ := pem.Decode(buf)
    97  	var pubKey dsa.PublicKey
    98  	_, err = asn1.Unmarshal(block.Bytes, &pubKey)
    99  	if err != nil {
   100  		return false, err
   101  	}
   102  	flag := dsa.Verify(&pubKey, []byte(content), r, s)
   103  	return flag, nil
   104  }