// create a new captcha id func (c *Captcha) CreateCaptcha() (string, error) { id := string(com.RandomCreateBytes(15)) if err := c.store.Put(c.key(id), c.genRandChars(), c.Expiration); err != nil { return "", err } return id, nil }
// generateRandomKey creates a random key with the given strength. func generateRandomKey(strength int) []byte { k := make([]byte, strength) if n, err := io.ReadFull(rand.Reader, k); n != strength || err != nil { return com.RandomCreateBytes(strength) } return k }
func prepareOptions(options []Options) Options { var opt Options if len(options) > 0 { opt = options[0] } // Defaults. if len(opt.Secret) == 0 { opt.Secret = string(com.RandomCreateBytes(10)) } if len(opt.Header) == 0 { opt.Header = "X-CSRFToken" } if len(opt.Form) == 0 { opt.Form = "_csrf" } if len(opt.Cookie) == 0 { opt.Cookie = "_csrf" } if len(opt.CookiePath) == 0 { opt.CookiePath = "/" } if len(opt.SessionKey) == 0 { opt.SessionKey = "uid" } opt.oldSeesionKey = "_old_" + opt.SessionKey if opt.ErrorFunc == nil { opt.ErrorFunc = func(w http.ResponseWriter) { http.Error(w, "Invalid csrf token.", http.StatusBadRequest) } } return opt }
func BenchmarkNewImage(b *testing.B) { b.StopTimer() d := com.RandomCreateBytes(6, defaultChars...) b.StartTimer() for i := 0; i < b.N; i++ { NewImage(d, stdWidth, stdHeight) } }
func (u *User) AddProject(p *Project) *User { if p.Id == "" { key := com.RandomCreateBytes(10) // TODO: Check key already exists p.Id = string(key) u.Projects = append(u.Projects, p) } else { u.UpdateProject(p) } return u }
func BenchmarkImageWriteTo(b *testing.B) { b.StopTimer() d := com.RandomCreateBytes(6, defaultChars...) b.StartTimer() counter := &byteCounter{} for i := 0; i < b.N; i++ { img := NewImage(d, stdWidth, stdHeight) img.WriteTo(counter) b.SetBytes(counter.n) counter.n = 0 } }
// generate rand chars with default chars func (c *Captcha) genRandChars() string { return string(com.RandomCreateBytes(c.ChallengeNums, defaultChars...)) }
func newSessionService() { SessionProvider = Cfg.MustValueRange("session", "PROVIDER", "memory", []string{"memory", "file", "redis", "mysql"}) SessionConfig = new(session.Config) SessionConfig.ProviderConfig = strings.Trim(Cfg.MustValue("session", "PROVIDER_CONFIG"), "\" ") SessionConfig.CookieName = Cfg.MustValue("session", "COOKIE_NAME", "i_like_gogits") SessionConfig.CookiePath = AppSubUrl SessionConfig.Secure = Cfg.MustBool("session", "COOKIE_SECURE") SessionConfig.EnableSetCookie = Cfg.MustBool("session", "ENABLE_SET_COOKIE", true) SessionConfig.Gclifetime = Cfg.MustInt64("session", "GC_INTERVAL_TIME", 86400) SessionConfig.Maxlifetime = Cfg.MustInt64("session", "SESSION_LIFE_TIME", 86400) SessionConfig.SessionIDHashFunc = Cfg.MustValueRange("session", "SESSION_ID_HASHFUNC", "sha1", []string{"sha1", "sha256", "md5"}) SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "SESSION_ID_HASHKEY", string(com.RandomCreateBytes(16))) if SessionProvider == "file" { os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm) } log.Info("Session Service Enabled") }