Digital signature process

1. Open the private key file of the disk

2. Read the contents of the private key file

3. Use pem to decode data and obtain pem.Block structure variable

4. X509 parses data into a private key structure to obtain a private key

Create a hash object

6. Add data to the hash object

7. Calculate the hash

8. Use functions in RSA to sign hash values

Digital authentication process

1. Open the public key file of the disk

2. Use pem to decode the pem.Block structure variable

3. Use X509 to parse variables in pem.Block to obtain a public key interface

4. Perform type assertion to obtain the public key structure

5. Hash the original message (the same hash algorithm used by the signature algorithm)

  • Create a hash interface
  • Add data
  • Hash algorithm

6. Signature authentication

Digital signature application in GO

package main

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/pem"
	"os"
)

/ / RSA signature
func SignRSA(plainText []byte, priFileName string) []byte {
	//1. Open the private key file of the disk
	file, err := os.Open(priFileName)
	iferr ! =nil {
		panic(err)
	}
	defer file.Close()
	//2. Read the contents of the private key file
	fileInfo, err := file.Stat()
	iferr ! =nil {
		panic(err)
	}
	buf := make([]byte, fileInfo.Size())
	_, err = file.Read(buf)
	iferr ! =nil {
		panic(err)
	}
	//3. Use pem to decode data to obtain pem.Block structure variable
	block, _ := pem.Decode(buf)
	X509 parses data into a private key structure to get a private key
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	iferr ! =nil {
		panic(err)
	}
	// create a hash object
	hash := sha256.New()
	//6. Add data to hash object
	_, err = hash.Write(plainText)
	iferr ! =nil {
		panic(err)
	}
	//7. Calculate the hash value
	hashed := hash.Sum(nil)
	Use functions in RSA to sign hash values
	signText, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
	iferr ! =nil {
		panic(err)
	}
	return signText
}

func VerifyRSA(plainText, signText []byte, pubFileName string) bool {
	//1. Open the public key file of the disk
	file, err := os.Open(pubFileName)
	iferr ! =nil {
		panic(err)
	}
	defer file.Close()
	fileInfo, err := file.Stat()
	iferr ! =nil {
		panic(err)
	}
	buf := make([]byte ,fileInfo.Size())
	_, err = file.Read(buf)
	iferr ! =nil {
		panic(err)
	}
	//2. Use pem to decode the pem.Block structure variable
	block, _ := pem.Decode(buf)
	//3. Use X509 to parse variables in pem.Block to get a public key interface
	pubKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
	iferr ! =nil {
		panic(err)
	}
	//4. Perform type assertion to obtain the public key structure
	publicKey := pubKeyInterface.(*rsa.PublicKey)
	//5. Hash the original message (the same hash used by the signature algorithm)
	//
	//* Create a hash interface
	hash := sha256.New()
	//* Add data
	hash.Write(plainText)
	//* hash operation
	hasded := hash.Sum(nil)
	//
	//6. Signature authentication
	err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hasded, signText)
	iferr ! =nil {
		return false
	}
	return true
}

func main (a){
	src := []byte("### digital signature \n\n#### Digital signature process \n\n> 1. Open the disk's private key file \n> 2. Read the contents of the private key file \n> 3. X509 parses the data into the private key structure to obtain the private key \n> 5. Create a hash object \n> 6. Add data to the hash object \n> 7. Calculate the hash value \n> 8. The process of digital authentication \n\n> 1 using functions in RSA to sign hash values \n\n####. Open the disk's public key file \n> 2. Decode with pem to obtain the pem.Block structure variable \n> 3. Parsing variables in pem.Block using X509 yields a public key interface \n> 4. Perform type assertion to obtain public key structure \n> 5. Hash the original message (the same hash algorithm used by the signature algorithm) \n>\n> * Create hash interface \n> * Add data \n> * hash \n>\n> 6. Signature authentication")
	signText := SignRSA(src, "private.pem")
	flag := VerifyRSA(src, signText, "public.pem")
	fmt.Println("Signature inspection Result:", flag)
}
Copy the code