comparison go/crypto.go @ 13:f5ffc34f045a

manage DB.
author pyon@macmini
date Wed, 14 Nov 2018 19:43:40 +0900
parents e3b10fb860b3
children
comparison
equal deleted inserted replaced
12:240752cbe11b 13:f5ffc34f045a
1 /* 1 /*
2 crypto.go : crypto-program. 2 crypto.go : crypto-program.
3 Version : 0.0 3 Version : 1.1
4 Last Change: 2018-10-19 金 15:58:10. 4 Last Change: 2018-11-14 水 13:27:48.
5 5
6 install to: rsearcher_root/ 6 install to: rsearcher_root/
7 server_root/ 7 server_root/
8 */ 8 */
9 package main 9 package main
24 ) 24 )
25 25
26 var version string 26 var version string
27 27
28 func init() { 28 func init() {
29 version = "1.0" 29 version = "1.1" // output file version
30 } 30 }
31 31
32 func main() { 32 func main() {
33 salt := flag.String( "s", "dummysalt", "salt." ) 33 salt := flag.String( "s", "dummysalt", "salt." )
34 enca := flag.String( "a", "", "encrypt plaintext." ) 34 enca := flag.String( "a", "", "encrypt plaintext." )
37 37
38 key := flag.String( "k", "1234567890abcdef1234567890abcdef", "key" ) // len = 32 38 key := flag.String( "k", "1234567890abcdef1234567890abcdef", "key" ) // len = 32
39 encr := flag.String( "e", "", "encrypt hhs." ) 39 encr := flag.String( "e", "", "encrypt hhs." )
40 decr := flag.String( "d", "", "decrypt hhs." ) 40 decr := flag.String( "d", "", "decrypt hhs." )
41 ghhs := flag.String( "f", "", "get hhs info." ) 41 ghhs := flag.String( "f", "", "get hhs info." )
42
43 opf := flag.String( "o", "", "output file." )
42 44
43 pver := flag.Bool( "v", false, "print version." ) 45 pver := flag.Bool( "v", false, "print version." )
44 46
45 flag.Parse() 47 flag.Parse()
46 48
136 } 138 }
137 139
138 stream := cipher.NewCTR( block, iv ) 140 stream := cipher.NewCTR( block, iv )
139 stream.XORKeyStream( ciphertext[ aes.BlockSize: ], plaintext ) 141 stream.XORKeyStream( ciphertext[ aes.BlockSize: ], plaintext )
140 142
141 fmt.Printf( "%s", ciphertext ) 143 //fmt.Printf( "%s", ciphertext )
144 output( *opf, string( ciphertext ) )
142 os.Exit( 0 ) 145 os.Exit( 0 )
143 } 146 }
144 147
145 // decrypt file 148 // decrypt file
146 if *decr != "" { 149 if *decr != "" {
152 iv := ciphertext[ :aes.BlockSize ] 155 iv := ciphertext[ :aes.BlockSize ]
153 plaintext := make( []byte, len( ciphertext[ aes.BlockSize: ] ) ) 156 plaintext := make( []byte, len( ciphertext[ aes.BlockSize: ] ) )
154 stream := cipher.NewCTR( block, iv ) 157 stream := cipher.NewCTR( block, iv )
155 stream.XORKeyStream( plaintext, ciphertext[ aes.BlockSize: ] ) 158 stream.XORKeyStream( plaintext, ciphertext[ aes.BlockSize: ] )
156 159
157 fmt.Printf( "%s", plaintext ) 160 //fmt.Printf( "%s", plaintext )
161 output( *opf, string( plaintext ) )
158 os.Exit( 0 ) 162 os.Exit( 0 )
159 } 163 }
160 164
161 /* Etc */ 165 /* Etc */
162 if *ghhs != "" { 166 if *ghhs != "" {
173 h := sha256.New() 177 h := sha256.New()
174 h.Write( []byte( text ) ) 178 h.Write( []byte( text ) )
175 return fmt.Sprintf( "%x", h.Sum( nil ) ) 179 return fmt.Sprintf( "%x", h.Sum( nil ) )
176 } 180 }
177 181
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