-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (79 loc) · 1.85 KB
/
main.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
package main
import (
crand "crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"time"
)
type Person struct {
Name string `json:"name"`
Email string `json:"email"`
}
func obfuscateEmail(email string) string {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return email
}
local := parts[0]
if len(local) <= 2 {
return email
}
return local[:2] + strings.Repeat("*", len(local)-2) + "@" + parts[1]
}
func obfuscateName(name string) string {
runes := []rune(name)
length := len(runes)
if length <= 2 {
return name
}
return string(runes[0]) + strings.Repeat("*", length-2) + string(runes[length-1])
}
func randomFileName() string {
bytes := make([]byte, 16)
if _, err := crand.Read(bytes); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(bytes) + ".txt"
}
func main() {
// Read the input.json file
jsonFile, err := ioutil.ReadFile("input.json")
if err != nil {
log.Fatal(err)
}
// Parse the JSON data
var people []Person
err = json.Unmarshal(jsonFile, &people)
if err != nil {
log.Fatal(err)
}
// Set the random seed
rand.Seed(time.Now().UnixNano())
// Randomly select a person
if len(people) > 0 {
randomIndex := rand.Intn(len(people))
randomName := obfuscateName(people[randomIndex].Name)
randomEmail := obfuscateEmail(people[randomIndex].Email)
fmt.Println("Randomly selected name and email:", randomName, randomEmail)
// Save the actual name and email to a file
fileName := randomFileName()
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.WriteString("Name: " + people[randomIndex].Name + "\nEmail: " + people[randomIndex].Email + "\n")
if err != nil {
log.Fatal(err)
}
fmt.Println("Actual name and email saved to:", fileName)
} else {
fmt.Println("No people data found.")
}
}