github.com/haraldrudell/parl@v0.4.176/parlca/text.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parlca
     7  
     8  import (
     9  	"crypto/sha1"
    10  	"crypto/sha256"
    11  	"encoding/hex"
    12  	"time"
    13  
    14  	"github.com/haraldrudell/parl"
    15  )
    16  
    17  const (
    18  	copyright = "Generated on %s by github.com/haraldrudell/parl\n" +
    19  		"parl: (c) 2018-present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)\n"
    20  	fingerPrint  = "sha256 fingerprint: "
    21  	fingerPrint1 = "sha1 fingerprint: "
    22  	pemNewline   = "\n"
    23  )
    24  
    25  func PemText(data ...[]byte) (pemText string) {
    26  	pemText = parl.Sprintf(copyright, time.Now().Format(parl.Rfc3339s))
    27  
    28  	// calculate sha256 fingerprint
    29  	if len(data) > 0 {
    30  		hashBytes := sha256.Sum256(data[0]) // [32]byte
    31  		pemText += fingerPrint + hex.EncodeToString(hashBytes[:4]) + pemNewline
    32  		if len(data) > 1 {
    33  			hashBytes1 := sha1.Sum(data[1])
    34  			pemText += fingerPrint1 + hex.EncodeToString(hashBytes1[:4]) + pemNewline
    35  		}
    36  	}
    37  	return
    38  }