Skip to content

Commit df699b1

Browse files
authored
Merge pull request #181 from finbox-in/feature/add-csharp-salt-generation-logic
Add C# Salt Generation
2 parents e7d618e + 3fc6c32 commit df699b1

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

docs/device-connect/salt-generation.md

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ Salt is generated at the client side for Authentication
55
## Calculate salt
66

77
Salt is calculated as follows:
8+
89
1. A = Create MD5 hash of `CUSTOMER_ID`
910
2. B = Concatenate string of A and `SERVER_HASH` shared by FinBox.
1011
3. C = Create an SHA-256 hash of B
1112
4. Salt = base64 encoded version of C
1213

1314
Sample code for salt generation in different languages:
1415

15-
<CodeSwitcher :languages="{python:'Python',go:'Go',java:'Java',php:'PHP',ruby:'Ruby',javascript:'JavaScript'}">
16+
<CodeSwitcher :languages="{python:'Python',go:'Go',java:'Java',csharp:'C#',php:'PHP',ruby:'Ruby',javascript:'JavaScript'}">
1617
<template v-slot:java>
1718

1819
```java
@@ -30,7 +31,7 @@ public class SaltGeneration {
3031
private static String CUSTOMER_ID = "<CUSTOMER_ID>";
3132
private static String SERVER_HASH = "<SERVER_HASH>";
3233

33-
private static String getSaltForBody() {
34+
public static String getSaltForBody() {
3435
String hashedOutput = getMd5Hash(CUSTOMER_ID);
3536
String concatString = hashedOutput + SERVER_HASH;
3637
String shaOutput = get256Encoded(concatString);
@@ -81,6 +82,66 @@ public class SaltGeneration {
8182
```
8283

8384
</template>
85+
86+
<template v-slot:csharp>
87+
88+
```csharp
89+
using System;
90+
using System.Security.Cryptography;
91+
using System.Text;
92+
93+
public class SaltGeneration {
94+
95+
private static string HEX_255 = "x2";
96+
private static string CUSTOMER_ID = "<CUSTOMER_ID>";
97+
private static string SERVER_HASH = "<SERVER_HASH>";
98+
99+
public static string CreateSalt() {
100+
string customerHash = CalculateMD5(CUSTOMER_ID).ToUpper();
101+
string intermediateHash = customerHash + SERVER_HASH;
102+
string shaOutput = CalculateSHA256(intermediateHash);
103+
return HexToBase64(shaOutput);
104+
}
105+
106+
private static string CalculateMD5(string s) {
107+
using(MD5 md5 = MD5.Create()) {
108+
byte[] inputBytes = Encoding.UTF8.GetBytes(s);
109+
byte[] hashBytes = md5.ComputeHash(inputBytes);
110+
111+
StringBuilder sb = new StringBuilder();
112+
for (int i = 0; i < hashBytes.Length; i++) {
113+
sb.Append(hashBytes[i].ToString(HEX_255));
114+
}
115+
return sb.ToString();
116+
}
117+
}
118+
119+
private static string CalculateSHA256(string input) {
120+
using(SHA256 sha256 = SHA256.Create()) {
121+
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
122+
byte[] hashBytes = sha256.ComputeHash(inputBytes);
123+
124+
StringBuilder sb = new StringBuilder();
125+
for (int i = 0; i < hashBytes.Length; i++) {
126+
sb.Append(hashBytes[i].ToString(HEX_255));
127+
}
128+
return sb.ToString();
129+
}
130+
}
131+
132+
private static string HexToBase64(string hexString) {
133+
int NumberChars = hexString.Length;
134+
byte[] bytes = new byte[NumberChars / 2];
135+
for (int i = 0; i < NumberChars; i += 2) {
136+
bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
137+
}
138+
return Convert.ToBase64String(bytes);
139+
}
140+
}
141+
```
142+
143+
</template>
144+
84145
<template v-slot:python>
85146

86147
```python
@@ -105,22 +166,22 @@ def create_salt(customer_id, server_hash):
105166

106167
```go
107168
import (
108-
"crypto/md5"
109-
"crypto/sha256"
110-
"fmt"
111-
"encoding/base64"
112-
"encoding/hex"
113-
"strings"
169+
"crypto/md5"
170+
"crypto/sha256"
171+
"fmt"
172+
"encoding/base64"
173+
"encoding/hex"
174+
"strings"
114175
)
115176
func GetSaltForCustomer(customerId string, serverHash string) string {
116177
hasher := md5.New()
117-
hasher.Write([]byte(customerId))
118-
hexHasher := hex.EncodeToString(hasher.Sum(nil))
119-
data := strings.ToUpper(hexHasher) + serverHash
120-
newSha256 := sha256.New()
121-
newSha256.Write([]byte(data))
178+
hasher.Write([]byte(customerId))
179+
hexHasher := hex.EncodeToString(hasher.Sum(nil))
180+
data := strings.ToUpper(hexHasher) + serverHash
181+
newSha256 := sha256.New()
182+
newSha256.Write([]byte(data))
122183
finalData := base64.StdEncoding.EncodeToString(newSha256.Sum(nil))
123-
return finalData
184+
return finalData
124185
}
125186
```
126187

@@ -175,7 +236,6 @@ function create_salt(customer_id, server_hash) {
175236

176237
</CodeSwitcher>
177238

178-
179239
## Debug Salt Generation
180240

181241
You can cross check each individual step of your salt generation logic by using the following parameters
@@ -185,17 +245,14 @@ customer_id = 82169C6312B50CA8233482169F9F288F812B5C02114A6A74E9A62
185245
server_hash = 5f8cd80c69a34b9785dc66298eabe95b
186246
```
187247

188-
189-
**Step A Result - Hexdigest of MD5Hash**
248+
**Step A Result - Hexdigest of MD5Hash**
190249

191250
`7B85689C14D32209779241F14A09C29B`
192251

193-
194-
**Step B Result - Intermediate Hash**
252+
**Step B Result - Intermediate Hash**
195253

196254
`7B85689C14D32209779241F14A09C29B5f8cd80c69a34b9785dc66298eabe95b`
197255

198-
199256
**Step C Result - Hexdigest Version**
200257

201258
`2a2e163b66dbcd838bd6d122e17038e90f2ba5c0b6ca295364c84e19746ca8e4`

0 commit comments

Comments
 (0)