github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/utils/verificationcode.go (about)

     1  /*
     2   * Copyright (c) 2023-present unTill Pro, Ltd.
     3   * @author Denis Gribanov
     4   */
     5  
     6  package coreutils
     7  
     8  import "crypto/rand"
     9  
    10  // generates cryptographically secure random verification code. Len - 6 bytes, each value is 0-9
    11  func EmailVerificationCode() (verificationCode string, err error) {
    12  	verificationCodeBytes := make([]byte, emailVerificationCodeLength)
    13  	if _, err := rand.Read(verificationCodeBytes); err != nil {
    14  		// notest
    15  		return "", err
    16  	}
    17  
    18  	// compress range 0..255 -> 0..9
    19  	for i := 0; i < len(verificationCodeBytes); i++ {
    20  		verificationCodeBytes[i] = emailVerificationCodeSymbols[int(float32(verificationCodeBytes[i])/byteRangeToEmailVerifcationSymbolsRangeCoeff)]
    21  	}
    22  	return string(verificationCodeBytes), nil
    23  }