|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Private/public key operations. |
|
4
|
|
|
* |
|
5
|
|
|
* @package ThreemaGateway |
|
6
|
|
|
* @author rugk |
|
7
|
|
|
* @copyright Copyright (c) 2015-2016 rugk |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class ThreemaGateway_Helper_Key |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the public key of a Threema ID. |
|
15
|
|
|
* |
|
16
|
|
|
* XenForo template helper: threemaidpubkey. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $threemaid Threema ID |
|
19
|
|
|
* @throws XenForo_Exception |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function getPublic($threemaid) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($threemaid == '') { |
|
25
|
|
|
return ''; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @var ThreemaGateway_Handler_Action_GatewayServer $gatewayHandlerServer */ |
|
29
|
|
|
$gatewayHandlerServer = new ThreemaGateway_Handler_Action_GatewayServer; |
|
30
|
|
|
|
|
31
|
|
|
try { |
|
32
|
|
|
$publicKey = $gatewayHandlerServer->fetchPublicKey($threemaid); |
|
33
|
|
|
} catch (Exception $e) { |
|
34
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_threema_id_does_not_exist') . ' ' . $e->getMessage()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $publicKey; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the short (user friendly) hash of the public key of a Threema ID. |
|
42
|
|
|
* |
|
43
|
|
|
* XenForo template helper: threemaidpubkeyshort. |
|
44
|
|
|
* Do not confuse this with {@link getUserDisplay()}. Here you have to pass |
|
45
|
|
|
* the Threema Id! |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $threemaid Threema ID |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function getPublicShort($threemaid) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($threemaid == '') { |
|
53
|
|
|
return ''; |
|
54
|
|
|
} |
|
55
|
|
|
return self::getUserDisplay(self::getPublic($threemaid)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Checks whether a public key is valid. |
|
60
|
|
|
* |
|
61
|
|
|
* XenForo template helper: threemaisvalidpubkey. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $pubKey Threema ID |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function checkPublic($pubKey) |
|
67
|
|
|
{ |
|
68
|
|
|
return self::check($pubKey, 'public:'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Checks whether a HEX-key is valid. |
|
73
|
|
|
* |
|
74
|
|
|
* XenForo template helper: threemaisvalidkey. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $publicKey the public key in hex |
|
77
|
|
|
* @param string $suffix optional suffix (usually 'private:' or 'public:') (default: '') |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool whether the key is valid (true) or not (false) |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function check($publicKey, $suffix = '') |
|
82
|
|
|
{ |
|
83
|
|
|
// RegExp: https://regex101.com/r/sU5tC8/1 |
|
84
|
|
|
return (preg_match('/^(' . $suffix . ')?[[:alnum:]]{64}$/', $publicKey) === 1); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the short user-friendly hash of the public key. |
|
89
|
|
|
* |
|
90
|
|
|
* XenForo template helper: threemashortpubkey. |
|
91
|
|
|
* Do not confuse this with {@link getPublicShort()}. Here you have to pass |
|
92
|
|
|
* the public key! |
|
93
|
|
|
* This is the way the key is also displayed in the Threema app. For example |
|
94
|
|
|
* ECHOECHO is shown as `d30f795a904a213578baecc62c8611b5`. However the |
|
95
|
|
|
* full public key of it is: |
|
96
|
|
|
* `4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b` |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $publicKey The public key to format. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string 32 hex characters |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function getUserDisplay($publicKey) |
|
103
|
|
|
{ |
|
104
|
|
|
//force key to be binary |
|
105
|
|
|
if (ctype_alnum($publicKey)) { |
|
106
|
|
|
$keyConverter = new ThreemaGateway_Handler_Action_KeyConverter; |
|
107
|
|
|
$publicKey = $keyConverter->hexToBin($publicKey); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
//create and return short hash |
|
111
|
|
|
return substr(hash('sha256', $publicKey), 0, 32); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Removes the suffix if necessary. |
|
116
|
|
|
* |
|
117
|
|
|
* XenForo template helper: threemakeyremovesuffix. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $keyHex The key in hex |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function removeSuffix($keyHex) |
|
123
|
|
|
{ |
|
124
|
|
|
/** @var string $keyTypeCheck */ |
|
125
|
|
|
$keyTypeCheck = substr($keyHex, 0, 8); |
|
126
|
|
|
if ($keyTypeCheck == 'private:' || $keyTypeCheck == 'public:') { |
|
127
|
|
|
$keyHex = substr($keyHex, 8); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $keyHex; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|