/
util.go
131 lines (107 loc) · 2.95 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"strings"
"os"
"path"
"io"
"fmt"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"sort"
"it-chain/network/protos"
"it-chain/auth"
"math/big"
"crypto/rand"
)
var logger = GetLogger("util.go")
func CreateDirIfMissing(dirPath string) (error){
if !strings.HasSuffix(dirPath, "/") {
dirPath = dirPath + "/"
}
//logger.Debugf("CreateDirIfMissing [%s]", dirPath)
err := os.MkdirAll(path.Dir(dirPath), 0755)
if err != nil {
logger.Debugf("Error while creating dir [%s]", dirPath)
return err
}
return nil
}
// DirEmpty returns true if the dir at dirPath is empty
func DirEmpty(dirPath string) (bool, error) {
f, err := os.Open(dirPath)
if err != nil {
logger.Debugf("Error while opening dir [%s]: %s", dirPath, err)
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err
}
func ComputeSHA256(data []string) string {
sort.Strings(data)
arg := strings.Join(data, ",")
hash := sha256.New()
hash.Write([]byte(arg))
return hex.EncodeToString(hash.Sum(nil))
}
/**
gob encoder로 인코딩했을 때 문제점
1. empty slice(make 로 생성한거) 가 디코딩하면 nil 로 디코딩 됨.
ㄴ json marshal로 바꾸면서 해결
2. time.Time 값들은 뒤에 monotonic 파트가 없어짐.
2번은 문제가 안 될수도 있는데 테스트 실패의 원인..
*/
func Serialize(object interface{}) ([]byte, error) {
data, err := json.Marshal(object)
if err != nil {
panic(fmt.Sprintf("Error encoding : %s", err))
}
return data, nil
}
func Deserialize(serializedBytes []byte, object interface{}) error {
if len(serializedBytes) == 0 {
return nil
}
err := json.Unmarshal(serializedBytes, object)
if err != nil {
panic(fmt.Sprintf("Error decoding : %s", err))
}
return err
}
//이렇게 하는것이 과연 최선일까?..
func ToEnvelope(data interface {}, crpyto auth.Crypto, pubkey []byte) message.Envelope{
byte,err := json.Marshal(data)
if err != nil {
panic(fmt.Sprintf("Error encoding : %s", err))
}
envelope := message.Envelope{}
signed, err :=crpyto.Sign(byte,nil)
if err != nil {
logger.Println("Fail to sign : %s", err)
}
envelope.Payload = byte
envelope.Signature = signed
envelope.Pubkey = pubkey
return envelope
}
func CryptoRandomGeneration(min int64, max int64) int64 {
n, _ := rand.Int(rand.Reader, big.NewInt(max + 1 - min))
ret := n.Int64() + min
return ret
}