Vérification de signature
Voilà quelques exemples et informations très pratiques pour valider la signature des corps de requêtes envoyées par RDV-Solidarités.
  • En Ruby
1
require 'openssl'
2
secret = "secret"
3
data = "content"
4
mac = OpenSSL::HMAC.hexdigest("SHA256", secret, data)
5
puts(data) puts(mac)
Copied!
  • En Python
1
import hashlib
2
import hmac
3
import sys
4
5
def main():
6
content = sys.argv[len(sys.argv)-1]
7
h = hmac.new(str.encode('secret'), digestmod='sha256')
8
9
print(content)
10
h.update(str.encode(content))
11
print(h.hexdigest())
12
13
if __name__ == '__main__':
14
main()
Copied!
  • En NodeJS
1
const crypto = require('crypto')
2
const prefixIndex = process.argv.indexOf('--content') if (prefixIndex == -1 || process.argv.length <= prefixIndex + 1) { console.log('--content [content] are mandatory') process.exit(1) }
3
const hmac = crypto.createHmac('sha256', process.env.SHARED_SECRET) const content = process.argv[prefixIndex+1]
4
console.log(content)
5
hmac.update(content) console.log(hmac.digest('hex'))
Copied!
  • En C#
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Security.Cryptography;
6
using System.IO;
7
using System.Runtime.Remoting.Metadata.W3cXsd2001;
8
9
namespace SignatureValidation
10
{
11
class Program
12
{
13
// Pour l'utilisation de SoapHexBinary
14
// cf.
15
// https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/2556329#2556329
16
static void Main(string[] args)
17
{
18
int keyPrefix = Array.IndexOf(args, "--key");
19
int contentPrefix = Array.IndexOf(args, "--content");
20
int signaturePrefix = Array.IndexOf(args, "--signature");
21
22
string key = args[keyPrefix+1];
23
string content = args[contentPrefix+1];
24
25
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
26
HMACSHA256 hash = new HMACSHA256(keyBytes);
27
28
byte[] byteArray = Encoding.UTF8.GetBytes(content);
29
MemoryStream stream = new MemoryStream(byteArray);
30
31
byte[] signature = hash.ComputeHash(stream);
32
33
SoapHexBinary shb = new SoapHexBinary(signature);
34
string hexaRepresentation = shb.ToString();
35
System.Console.WriteLine(hexaRepresentation);
36
37
if (signaturePrefix>= 0) {
38
string providedSignature = args[signaturePrefix+1];
39
byte[] providedSignatureBytes = SoapHexBinary.Parse(providedSignature).Value;
40
41
System.Console.WriteLine(providedSignatureBytes.SequenceEqual(signature));
42
}
43
44
return;
45
}
46
}
47
}
Copied!
Dernière mise à jour 1mo ago
Exporter en PDF
Copier le lien