Repository navigation
Expand file tree
/
Copy pathgoLangHashCalculation.go
More file actions
106 lines (87 loc) · 2.94 KB
/
Copy pathgoLangHashCalculation.go
File metadata and controls
106 lines (87 loc) · 2.94 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
package services_iias
import (
"crypto/sha256"
"encoding/hex"
"encoding/xml"
"strings"
"github.com/ucarion/c14n"
)
func ExampleToCalculateHash(xmlPartnerCopyConditions, partnerHash string) (err error) {
str, erro := prepareConditions(xmlPartnerCopyConditions)
if erro != nil {
// error! Could not Apply Exclusive Canonicalization to Cooperation Conditions.
return
}
// Calculates the Hash
hash, err := calculateHash(str)
if err != nil {
// error! Calculated Hash is different than the one fetched from the IIA.
return
}
// Compares the calculated hash with the partner copy of the IIA's hash
if partnerHash == hash {
// error! Calculated Hash is different than the one fetched from the IIA.
return
}
return
}
/**
Applies Exclusive Cannonicalization to the XML of the Cooperation Conditions
Adds the XMLNS to the cooperation-conditions tags
Removes sending and receiving contacts from the conditions
**/
func prepareConditions(xmlPartnerCopyConditions string) (string, error) {
// Developer may need to remove XML prologue and handle partner prefixes in the tags
// Cannonicalize XML
decoder := xml.NewDecoder(strings.NewReader(xmlPartnerCopyConditions))
out, _ := c14n.Canonicalize(decoder)
coopConditionsXML := string(out)
// Add the xmlns to the cooperation-condition tag
coopConditionsXML = strings.ReplaceAll(coopConditionsXML, "<cooperation-conditions>", "<cooperation-conditions xmlns=\"https://github.com/erasmus-without-paper/ewp-specs-api-iias/blob/stable-v6/endpoints/get-response.xsd\"")
// Remove sending and receiving contacts from all conditions
for {
if strings.Contains(coopConditionsXML, "sending-contact>") {
sending := strings.SplitAfter(coopConditionsXML, "<sending-contact>")
sending2 := strings.SplitAfter(sending[1], "</sending-contact>")
sending_s := strings.ReplaceAll(sending[0], "<sending-contact>", "")
coopConditionsXML = sending_s + sending2[1]
if len(sending) > 2 {
var rec string
for i := 2; i < len(sending); i++ {
rec += sending[i]
}
coopConditionsXML += rec
}
} else {
break
}
}
for {
if strings.Contains(coopConditionsXML, "receiving-contact>") {
receiving := strings.SplitAfter(coopConditionsXML, "<receiving-contact>")
receiving2 := strings.SplitAfter(receiving[1], "</receiving-contact>")
receiving_s := strings.ReplaceAll(receiving[0], "<receiving-contact>", "")
coopConditionsXML = receiving_s + receiving2[1]
if len(receiving) > 2 {
var rec string
for i := 2; i < len(receiving); i++ {
rec += receiving[i]
}
coopConditionsXML += rec
}
} else {
break
}
}
return coopConditionsXML, nil
}
/*
Calculates the SHA 256 of a string
In EWP context is used to calculate the Hash signature of the Cooperation Conditions
*/
func calculateHash(toBeCalculated string) (finalSHA string, err error) {
hash := sha256.Sum256([]byte(toBeCalculated))
slicedSHA := hash[:]
finalSHA = hex.EncodeToString(slicedSHA)
return
}