-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontents.go
60 lines (48 loc) · 1.37 KB
/
contents.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
package wadlib
import (
"errors"
)
var (
ErrInvalidIndex = errors.New("index does not exist within WAD")
)
// GetContent returns the data for the given index.
func (w *WAD) GetContent(index int) ([]byte, error) {
// Ensure the index is valid.
if index > len(w.Data) {
return nil, ErrInvalidIndex
}
titleKey := w.Ticket.GetTitleKey()
return w.Data[index].DecryptData(titleKey)
}
// UpdateContent updates the data at the given index with the given content.
func (w *WAD) UpdateContent(index int, contents []byte) error {
// Ensure the index is valid.
if index > len(w.Data) {
return ErrInvalidIndex
}
titleKey := w.Ticket.GetTitleKey()
w.Data[index].UpdateData(contents, titleKey)
return nil
}
// ChangeTitleKey updates the ticket to contain the given title key,
// and re-encrypts all data to match.
func (w *WAD) ChangeTitleKey(updatedKey [16]byte) error {
// Obtain the current title key.
oldTitleKey := w.Ticket.GetTitleKey()
// Save existing title data to a separate array.
titleData := make(map[int][]byte)
for index, data := range w.Data {
decrypted, err := data.DecryptData(oldTitleKey)
if err != nil {
return err
}
titleData[index] = decrypted
}
// Update our title key.
w.Ticket.UpdateTitleKey(updatedKey)
// Encrypt our separate title data.
for index, data := range titleData {
w.Data[index].UpdateData(data, updatedKey)
}
return nil
}