0
|
1 /*
|
|
2 crypto.go : crypto-program.
|
|
3 Version : 0.0
|
5
|
4 Last Change: 2018-10-19 金 15:58:10.
|
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() {
|
|
29 version = "1.0"
|
|
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
|
5
|
43 pver := flag.Bool( "v", false, "print version." )
|
|
44
|
0
|
45 flag.Parse()
|
|
46
|
5
|
47 if *pver {
|
|
48 fmt.Println( "crypto [", version, "]" )
|
|
49 os.Exit( 0 )
|
|
50 }
|
|
51
|
|
52 /* Hash ( 1 word ) */
|
0
|
53 if *enca != "" {
|
|
54 buf := *salt + enc_sha256( *enca ) + *salt
|
|
55 fmt.Println( enc_sha256( buf ) )
|
|
56 os.Exit( 0 ) // done.
|
|
57 }
|
|
58
|
5
|
59 /* Hash ( csv ) */
|
0
|
60 if *encb != "" {
|
3
|
61 f, err := os.Open( *encb ); if err != nil {
|
|
62 log.Fatal( err )
|
|
63 }
|
|
64 defer f.Close()
|
|
65
|
|
66 r := csv.NewReader( f )
|
|
67 for {
|
|
68 record, err := r.Read()
|
|
69 if err == io.EOF {
|
|
70 break
|
|
71 }
|
|
72 if err != nil {
|
|
73 log.Fatal( err )
|
|
74 }
|
|
75 buf := *salt + enc_sha256( record[ 1 ] ) + *salt
|
|
76 fmt.Println( record[ 0 ], enc_sha256( buf ) )
|
|
77 }
|
|
78 os.Exit( 0 ) // done.
|
|
79 }
|
|
80
|
5
|
81 /* Password Check */
|
3
|
82 if *coll != "" {
|
|
83 f, err := os.Open( *coll ); if err != nil {
|
|
84 log.Fatal( err )
|
|
85 }
|
|
86 defer f.Close()
|
|
87
|
|
88 if flag.NArg() != 2 {
|
|
89 fmt.Fprintf( os.Stderr, "bad argument\n" )
|
|
90 os.Exit( 1 )
|
|
91 }
|
|
92 user := flag.Arg( 0 )
|
|
93 pw := flag.Arg( 1 )
|
|
94
|
|
95 r := csv.NewReader( f )
|
|
96 r.Comma = ' '
|
0
|
97 for {
|
|
98 record, err := r.Read()
|
|
99 if err == io.EOF {
|
|
100 break
|
|
101 }
|
|
102 if err != nil {
|
|
103 log.Fatal( err )
|
|
104 }
|
3
|
105
|
|
106 if record[ 0 ] == user {
|
|
107 buf := *salt + enc_sha256( pw ) + *salt
|
|
108 if record[ 1 ] == enc_sha256( buf ) {
|
|
109 fmt.Println( "valid" )
|
|
110 os.Exit( 39 )
|
|
111 }
|
|
112 }
|
0
|
113 }
|
3
|
114 os.Exit( 0 ) // done.
|
0
|
115 }
|
|
116
|
5
|
117 /* Encrypto & Decrypto */
|
|
118 k, _ := hex.DecodeString( *key ) // len = 32
|
|
119 block, err := aes.NewCipher( k )
|
|
120 if err != nil {
|
|
121 panic( err )
|
|
122 }
|
3
|
123
|
5
|
124 // encrypto file
|
|
125 if *encr != "" {
|
|
126 plaintext, err := ioutil.ReadFile( *encr ) // 入力CSVは S-JIS
|
3
|
127 if err != nil {
|
|
128 log.Fatal( err )
|
|
129 }
|
|
130
|
|
131 // IV は公開してもいいので先頭につけておく
|
|
132 ciphertext := make( []byte, aes.BlockSize + len( plaintext ) )
|
|
133 iv := ciphertext[ :aes.BlockSize ]
|
|
134 if _, err := io.ReadFull( rand.Reader, iv ); err != nil {
|
|
135 panic( err )
|
|
136 }
|
|
137
|
|
138 stream := cipher.NewCTR( block, iv )
|
|
139 stream.XORKeyStream( ciphertext[ aes.BlockSize: ], plaintext )
|
|
140
|
5
|
141 fmt.Printf( "%s", ciphertext )
|
3
|
142 os.Exit( 0 )
|
0
|
143 }
|
|
144
|
5
|
145 // decrypt file
|
|
146 if *decr != "" {
|
|
147 ciphertext, err := ioutil.ReadFile( *decr )
|
|
148 if err != nil {
|
|
149 log.Fatal( err )
|
|
150 }
|
|
151
|
|
152 iv := ciphertext[ :aes.BlockSize ]
|
|
153 plaintext := make( []byte, len( ciphertext[ aes.BlockSize: ] ) )
|
|
154 stream := cipher.NewCTR( block, iv )
|
|
155 stream.XORKeyStream( plaintext, ciphertext[ aes.BlockSize: ] )
|
|
156
|
|
157 fmt.Printf( "%s", plaintext )
|
|
158 os.Exit( 0 )
|
0
|
159 }
|
|
160
|
5
|
161 /* Etc */
|
|
162 if *ghhs != "" {
|
|
163 os.Exit( 0 )
|
|
164 }
|
|
165
|
|
166 /* Error */
|
0
|
167 fmt.Fprintf( os.Stderr, "bad argument\n" )
|
|
168 os.Exit( 1 )
|
|
169 }
|
|
170
|
5
|
171 /* Hash Function */
|
0
|
172 func enc_sha256( text string ) string {
|
|
173 h := sha256.New()
|
|
174 h.Write( []byte( text ) )
|
|
175 return fmt.Sprintf( "%x", h.Sum( nil ) )
|
|
176 }
|
|
177
|