decrypt.go raw
1 package main
2
3 import (
4 "common/crypto/sha256"
5 "common/helpers"
6 )
7
8 // DMRecord represents a decrypted DM.
9 type DMRecord struct {
10 ID string
11 Peer string
12 From string
13 Content string
14 CreatedAt int64
15 Protocol string
16 EventID string
17 }
18
19 func (r *DMRecord) ToJSON() string {
20 return "{" +
21 "\"id\":" + jstr(r.ID) +
22 ",\"peer\":" + jstr(r.Peer) +
23 ",\"from\":" + jstr(r.From) +
24 ",\"content\":" + jstr(r.Content) +
25 ",\"created_at\":" + helpers.Itoa(r.CreatedAt) +
26 ",\"protocol\":" + jstr(r.Protocol) +
27 ",\"eventId\":" + jstr(r.EventID) +
28 "}"
29 }
30
31 func makeDMRecord(peer, from, content string, createdAt int64, protocol, eventID string) *DMRecord {
32 return &DMRecord{
33 ID: dmDedupID(peer, content, createdAt),
34 Peer: peer,
35 From: from,
36 Content: content,
37 CreatedAt: createdAt,
38 Protocol: protocol,
39 EventID: eventID,
40 }
41 }
42
43 func dmDedupID(peer, content string, createdAt int64) string {
44 contentHash := sha256.Sum([]byte(content))
45 window := helpers.Itoa(createdAt / 300)
46 combined := sha256.Sum([]byte(peer + helpers.HexEncode(contentHash[:]) + window))
47 return helpers.HexEncode(combined[:])
48 }
49