Skip to content

Commit 9629073

Browse files
author
Gyeongjun Paik
committed
Init: random election project
0 parents  commit 9629073

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module random_election

main.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
crand "crypto/rand"
5+
"encoding/hex"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"log"
10+
"math/rand"
11+
"os"
12+
"strings"
13+
"time"
14+
)
15+
16+
type Person struct {
17+
Name string `json:"name"`
18+
Email string `json:"email"`
19+
}
20+
21+
func obfuscateEmail(email string) string {
22+
parts := strings.Split(email, "@")
23+
if len(parts) != 2 {
24+
return email
25+
}
26+
27+
local := parts[0]
28+
if len(local) <= 2 {
29+
return email
30+
}
31+
32+
return local[:2] + strings.Repeat("*", len(local)-2) + "@" + parts[1]
33+
}
34+
35+
func obfuscateName(name string) string {
36+
runes := []rune(name)
37+
length := len(runes)
38+
if length <= 2 {
39+
return name
40+
}
41+
42+
return string(runes[0]) + strings.Repeat("*", length-2) + string(runes[length-1])
43+
}
44+
45+
func randomFileName() string {
46+
bytes := make([]byte, 16)
47+
if _, err := crand.Read(bytes); err != nil {
48+
log.Fatal(err)
49+
}
50+
return hex.EncodeToString(bytes) + ".txt"
51+
}
52+
53+
func main() {
54+
// Read the input.json file
55+
jsonFile, err := ioutil.ReadFile("input.json")
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
60+
// Parse the JSON data
61+
var people []Person
62+
err = json.Unmarshal(jsonFile, &people)
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
67+
// Set the random seed
68+
rand.Seed(time.Now().UnixNano())
69+
70+
// Randomly select a person
71+
if len(people) > 0 {
72+
randomIndex := rand.Intn(len(people))
73+
randomName := obfuscateName(people[randomIndex].Name)
74+
randomEmail := obfuscateEmail(people[randomIndex].Email)
75+
fmt.Println("Randomly selected name and email:", randomName, randomEmail)
76+
77+
// Save the actual name and email to a file
78+
fileName := randomFileName()
79+
file, err := os.Create(fileName)
80+
if err != nil {
81+
log.Fatal(err)
82+
}
83+
defer file.Close()
84+
85+
_, err = file.WriteString("Name: " + people[randomIndex].Name + "\nEmail: " + people[randomIndex].Email + "\n")
86+
if err != nil {
87+
log.Fatal(err)
88+
}
89+
90+
fmt.Println("Actual name and email saved to:", fileName)
91+
} else {
92+
fmt.Println("No people data found.")
93+
}
94+
}

0 commit comments

Comments
 (0)