1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
16 // Sign a given string with the app-configured secret key.
17 // If no secret key is set, returns the empty string.
18 // Return the signature in base64 (URLEncoding).
19 func Sign(message string) string {
20 if len(secretKey) == 0 {
23 mac := hmac.New(sha1.New, secretKey)
24 if _, err := io.WriteString(mac, message); err != nil {
25 utilLog.Error("WriteString failed", "error", err)
28 return hex.EncodeToString(mac.Sum(nil))
31 // Verify returns true if the given signature is correct for the given message.
32 // e.g. it matches what we generate with Sign()
33 func Verify(message, sig string) bool {
34 return hmac.Equal([]byte(sig), []byte(Sign(message)))
37 // ToBool method converts/assert value into true or false. Default is true.
38 // When converting to boolean, the following values are considered FALSE:
39 // - The integer value is 0 (zero)
40 // - The float value 0.0 (zero)
41 // - The complex value 0.0 (zero)
42 // - For string value, please refer `revel.Atob` method
43 // - An array, map, slice with zero elements
44 // - Boolean value returned as-is
46 func ToBool(val interface{}) bool {
51 v := reflect.ValueOf(val)
56 return Atob(v.String())
57 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
59 case reflect.Float32, reflect.Float64:
60 return v.Float() != 0.0
61 case reflect.Complex64, reflect.Complex128:
62 return v.Complex() != 0.0
63 case reflect.Array, reflect.Map, reflect.Slice:
67 // Return true by default
71 // Atob converts string into boolean. It is in-case sensitive
72 // When converting to boolean, the following values are considered FALSE:
73 // - The "" (empty) string
74 // - The "false" string
76 // - The "off" string,
77 // - The string "0" & "0.0"
78 func Atob(v string) bool {
79 switch strings.TrimSpace(strings.ToLower(v)) {
80 case "", "false", "off", "f", "0", "0.0":
84 // Return true by default