-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac.service.ts
49 lines (42 loc) · 1.1 KB
/
hmac.service.ts
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
import { Injectable } from '@angular/core';
@Injectable()
export class HmacService {
constructor() { }
public generate(body: Object): string {
let hash_str: string;
hash_str = this.iterate(this.sort(body));
console.log(hash_str);
return hash_str;
}
private iterate(object) {
const values = [];
for (const property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] === 'object') {
values.push(this.iterate(object[property]));
} else {
values.push(object[property]);
}
}
}
return values.join('*');
}
private sort(object: Object): Object {
const _ordered = {};
const _this = this;
Object.keys(object)
.sort()
.forEach(function(key, i) {
if (typeof object[key] === 'object'){
_ordered[key] = _this.sort(object[key]);
}else {
_ordered[key] = object[key] ;
}
});
return _ordered;
}
public sign(body: Object): Object {
body['sHMAC'] = this.generate(body);
return body;
}
}