0
|
1 /*
|
|
2 crypto.go : crypto-program.
|
13
|
3 Version : 1.1
|
|
4 Last Change: 2018-11-14 水 13:27:48.
|
0
|
5
|
|
6 install to: rsearcher_root/
|
|
7 server_root/
|
|
8 */
|
|
9 package main
|
|
10
|
|
11 import (
|
3
|
12 "crypto/aes"
|
|
13 "crypto/cipher"
|
|
14 "crypto/rand"
|
0
|
15 "crypto/sha256"
|
3
|
16 "encoding/csv"
|
|
17 "encoding/hex"
|
0
|
18 "fmt"
|
|
19 "flag"
|
3
|
20 "io"
|
|
21 "io/ioutil"
|
|
22 "log"
|
0
|
23 "os"
|
|
24 )
|
|
25
|
5
|
26 var version string
|
|
27
|
|
28 func init() {
|
13
|
29 version = "1.1" // output file version
|
5
|
30 }
|
|
31
|
0
|
32 func main() {
|
3
|
33 salt := flag.String( "s", "dummysalt", "salt." )
|
0
|
34 enca := flag.String( "a", "", "encrypt plaintext." )
|
|
35 encb := flag.String( "b", "", "encrypt csv-file." )
|
|
36 coll := flag.String( "c", "", "collate user/password." )
|
|
37
|
5
|
38 key := flag.String( "k", "1234567890abcdef1234567890abcdef", "key" ) // len = 32
|
0
|
39 encr := flag.String( "e", "", "encrypt hhs." )
|
5
|
40 decr := flag.String( "d", "", "decrypt hhs." )
|
3
|
41 ghhs := flag.String( "f", "", "get hhs info." )
|
0
|
42
|
13
|
43 opf := flag.String( "o", "", "output file." )
|
|
44
|
5
|
45 pver := flag.Bool( "v", false, "print version." )
|
|
46
|
0
|
47 flag.Parse()
|
|
48
|
5
|
49 if *pver {
|
|
50 fmt.Println( "crypto [", version, "]" )
|
|
51 os.Exit( 0 )
|
|
52 }
|
|
53
|
|
54 /* Hash ( 1 word ) */
|
0
|
55 if *enca != "" {
|
|
56 buf := *salt + enc_sha256( *enca ) + *salt
|
|
57 fmt.Println( enc_sha256( buf ) )
|
|
58 os.Exit( 0 ) // done.
|
|
59 }
|
|
60
|
5
|
61 /* Hash ( csv ) */
|
0
|
62 if *encb != "" {
|
3
|
63 f, err := os.Open( *encb ); if err != nil {
|
|
64 log.Fatal( err )
|
|
65 }
|
|
66 defer f.Close()
|
|
67
|
|
68 r := csv.NewReader( f )
|
|
69 for {
|
|
70 record, err := r.Read()
|
|
71 if err == io.EOF {
|
|
72 break
|
|
73 }
|
|
74 if err != nil {
|
|
75 log.Fatal( err )
|
|
76 }
|
|
77 buf := *salt + enc_sha256( record[ 1 ] ) + *salt
|
|
78 fmt.Println( record[ 0 ], enc_sha256( buf ) )
|
|
79 }
|
|
80 os.Exit( 0 ) // done.
|
|
81 }
|
|
82
|
5
|
83 /* Password Check */
|
3
|
84 if *coll != "" {
|
|
85 f, err := os.Open( *coll ); if err != nil {
|
|
86 log.Fatal( err )
|
|
87 }
|
|
88 defer f.Close()
|
|
89
|
|
90 if flag.NArg() != 2 {
|
|
91 fmt.Fprintf( os.Stderr, "bad argument\n" )
|
|
92 os.Exit( 1 )
|
|
93 }
|
|
94 user := flag.Arg( 0 )
|
|
95 pw := flag.Arg( 1 )
|
|
96
|
|
97 r := csv.NewReader( f )
|
|
98 r.Comma = ' '
|
0
|
99 for {
|
|
100 record, err := r.Read()
|
|
101 if err == io.EOF {
|
|
102 break
|
|
103 }
|
|
104 if err != nil {
|
|
105 log.Fatal( err )
|
|
106 }
|
3
|
107
|
|
108 if record[ 0 ] == user {
|
|
109 buf := *salt + enc_sha256( pw ) + *salt
|
|
110 if record[ 1 ] == enc_sha256( buf ) {
|
|
111 fmt.Println( "valid" )
|
|
112 os.Exit( 39 )
|
|
113 }
|
|
114 }
|
0
|
115 }
|
3
|
116 os.Exit( 0 ) // done.
|
0
|
117 }
|
|
118
|
5
|
119 /* Encrypto & Decrypto */
|
|
120 k, _ := hex.DecodeString( *key ) // len = 32
|
|
121 block, err := aes.NewCipher( k )
|
|
122 if err != nil {
|
|
123 panic( err )
|
|
124 }
|
3
|
125
|
5
|
126 // encrypto file
|
|
127 if *encr != "" {
|
|
128 plaintext, err := ioutil.ReadFile( *encr ) // 入力CSVは S-JIS
|
3
|
129 if err != nil {
|
|
130 log.Fatal( err )
|
|
131 }
|
|
132
|
|
133 // IV は公開してもいいので先頭につけておく
|
|
134 ciphertext := make( []byte, aes.BlockSize + len( plaintext ) )
|
|
135 iv := ciphertext[ :aes.BlockSize ]
|
|
136 if _, err := io.ReadFull( rand.Reader, iv ); err != nil {
|
|
137 panic( err )
|
|
138 }
|
|
139
|
|
140 stream := cipher.NewCTR( block, iv )
|
|
141 stream.XORKeyStream( ciphertext[ aes.BlockSize: ], plaintext )
|
|
142
|
13
|
143 //fmt.Printf( "%s", ciphertext )
|
|
144 output( *opf, string( ciphertext ) )
|
3
|
145 os.Exit( 0 )
|
0
|
146 }
|
|
147
|
5
|
148 // decrypt file
|
|
149 if *decr != "" {
|
|
150 ciphertext, err := ioutil.ReadFile( *decr )
|
|
151 if err != nil {
|
|
152 log.Fatal( err )
|
|
153 }
|
|
154
|
|
155 iv := ciphertext[ :aes.BlockSize ]
|
|
156 plaintext := make( []byte, len( ciphertext[ aes.BlockSize: ] ) )
|
|
157 stream := cipher.NewCTR( block, iv )
|
|
158 stream.XORKeyStream( plaintext, ciphertext[ aes.BlockSize: ] )
|
|
159
|
13
|
160 //fmt.Printf( "%s", plaintext )
|
|
161 output( *opf, string( plaintext ) )
|
5
|
162 os.Exit( 0 )
|
0
|
163 }
|
|
164
|
5
|
165 /* Etc */
|
|
166 if *ghhs != "" {
|
|
167 os.Exit( 0 )
|
|
168 }
|
|
169
|
|
170 /* Error */
|
0
|
171 fmt.Fprintf( os.Stderr, "bad argument\n" )
|
|
172 os.Exit( 1 )
|
|
173 }
|
|
174
|
5
|
175 /* Hash Function */
|
0
|
176 func enc_sha256( text string ) string {
|
|
177 h := sha256.New()
|
|
178 h.Write( []byte( text ) )
|
|
179 return fmt.Sprintf( "%x", h.Sum( nil ) )
|
|
180 }
|
|
181
|
13
|
182 /* Output Function */
|
|
183 func output( file, str string ) {
|
|
184 if file == "" {
|
|
185 fmt.Fprint( os.Stdout, str )
|
|
186 } else {
|
|
187 os.Remove( file )
|
|
188 f, err := os.OpenFile( file, os.O_RDWR|os.O_CREATE, 0644 )
|
|
189 if err != nil {
|
|
190 log.Fatal( err )
|
|
191 }
|
|
192 defer f.Close()
|
|
193 fmt.Fprint( f, str )
|
|
194 }
|
|
195 }
|
|
196
|