forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (114 loc) · 3.17 KB
/
main.go
File metadata and controls
132 lines (114 loc) · 3.17 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"os"
"time"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/carlmjohnson/requests"
"github.com/palantir/stacktrace"
)
func main() {
err := godotenv.Load("../../.env")
if err != nil {
log.Fatal("Error loading .env file")
}
sendSingle()
}
func bulkSend() {
var to []string
for i := 0; i < 100; i++ {
to = append(to, os.Getenv("HTTPSMS_TO_BULK"))
}
var responsePayload string
err := requests.
URL("/v1/messages/bulk-send").
Host("api.httpsms.com").
Header("x-api-key", os.Getenv("HTTPSMS_KEY_BULK")).
BodyJSON(&map[string]any{
"content": fmt.Sprintf("Bulk Load Test [%s]", time.Now().Format(time.RFC850)),
"from": os.Getenv("HTTPSMS_FROM_BULK"),
"to": to,
"request_id": fmt.Sprintf("load-%s", uuid.NewString()),
}).
ToString(&responsePayload).
Fetch(context.Background())
if err != nil {
log.Println(responsePayload)
log.Fatal(stacktrace.Propagate(err, "cannot create request"))
}
log.Println(responsePayload)
}
func sendSingle() {
for i := 0; i < 1; i++ {
var responsePayload string
err := requests.
URL("/v1/messages/send").
Host("api.httpsms.com").
Header("x-api-key", os.Getenv("HTTPSMS_KEY")).
BodyJSON(&map[string]any{
"content": fmt.Sprintf("This is a test text message [%d]", i),
"from": os.Getenv("HTTPSMS_FROM"),
"to": os.Getenv("HTTPSMS_TO"),
"encrypted": false,
"request_id": fmt.Sprintf("load-%s-%d", uuid.NewString(), i),
}).
ToString(&responsePayload).
Fetch(context.Background())
if err != nil {
log.Fatal(stacktrace.Propagate(err, "cannot create json payload"))
}
log.Println(responsePayload)
}
}
func encrypt(value string) string {
key := sha256.Sum256([]byte("Password123"))
iv := make([]byte, 16)
_, err := rand.Read(iv)
if err != nil {
log.Fatal(stacktrace.Propagate(err, "cannot generate iv"))
}
c := ase256(value, key[:], iv)
fmt.Println("iv", base64.StdEncoding.EncodeToString(iv))
fmt.Println("cypher", base64.StdEncoding.EncodeToString(c))
fmt.Println("cypher+iv", base64.StdEncoding.EncodeToString(append(iv, c...)))
return base64.StdEncoding.EncodeToString(append(iv, c...))
}
func decode(value string) string {
content, err := base64.StdEncoding.DecodeString(value)
if err != nil {
log.Fatal(err)
}
key := sha256.Sum256([]byte(os.Getenv("HTTPSMS_ENCRYPTION_KEY")))
iv := content[:16]
return ase256Decode(content[16:], key[:], iv)
}
func ase256(plaintext string, key []byte, bIV []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
text := []byte(plaintext)
stream := cipher.NewCFBEncrypter(block, bIV)
cypher := make([]byte, len(text))
stream.XORKeyStream(cypher, text)
return cypher
}
func ase256Decode(cipherText []byte, key []byte, iv []byte) (decryptedString string) {
// Create a new AES cipher with the key and encrypted message
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
// Decrypt the message
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherText, cipherText)
return string(cipherText)
}