mirror of
				https://github.com/woodpecker-ci/woodpecker.git
				synced 2025-10-31 00:37:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package model
 | |
| 
 | |
| import (
 | |
| 	"crypto/md5"
 | |
| 	"crypto/rand"
 | |
| 	"fmt"
 | |
| 	"io"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| // standard characters allowed in token string.
 | |
| var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
 | |
| 
 | |
| // default token length
 | |
| var length = 40
 | |
| 
 | |
| // generateToken generates random strings good for use in URIs to
 | |
| // identify unique objects.
 | |
| func generateToken() string {
 | |
| 	b := make([]byte, length)
 | |
| 	r := make([]byte, length+(length/4)) // storage for random bytes.
 | |
| 	clen := byte(len(chars))
 | |
| 	maxrb := byte(256 - (256 % len(chars)))
 | |
| 	i := 0
 | |
| 	for {
 | |
| 		io.ReadFull(rand.Reader, r)
 | |
| 		for _, c := range r {
 | |
| 			if c >= maxrb {
 | |
| 				// Skip this number to avoid modulo bias.
 | |
| 				continue
 | |
| 			}
 | |
| 			b[i] = chars[c%clen]
 | |
| 			i++
 | |
| 			if i == length {
 | |
| 				return string(b)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // helper function to create a Gravatar Hash
 | |
| // for the given Email address.
 | |
| func createGravatar(email string) string {
 | |
| 	email = strings.ToLower(strings.TrimSpace(email))
 | |
| 	hash := md5.New()
 | |
| 	hash.Write([]byte(email))
 | |
| 	return fmt.Sprintf("%x", hash.Sum(nil))
 | |
| }
 |