Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / libs.go
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.
4
5 package revel
6
7 import (
8         "crypto/hmac"
9         "crypto/sha1"
10         "encoding/hex"
11         "io"
12         "reflect"
13         "strings"
14 )
15
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 {
21                 return ""
22         }
23         mac := hmac.New(sha1.New, secretKey)
24         if _, err := io.WriteString(mac, message); err != nil {
25                 utilLog.Error("WriteString failed", "error", err)
26                 return ""
27         }
28         return hex.EncodeToString(mac.Sum(nil))
29 }
30
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)))
35 }
36
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
45 // - "nil" value
46 func ToBool(val interface{}) bool {
47         if val == nil {
48                 return false
49         }
50
51         v := reflect.ValueOf(val)
52         switch v.Kind() {
53         case reflect.Bool:
54                 return v.Bool()
55         case reflect.String:
56                 return Atob(v.String())
57         case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
58                 return v.Int() != 0
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:
64                 return v.Len() != 0
65         }
66
67         // Return true by default
68         return true
69 }
70
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
75 // - The "f" 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":
81                 return false
82         }
83
84         // Return true by default
85         return true
86 }