summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/WARegister.cpp
blob: 2bf0cb8b834e6046390c7af347801cf40e56a8be (plain)
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
/*
 * WARegister.cpp
 *
 */

#include "../common.h" // #TODO Remove Miranda-dependency

#include "WARegister.h"
#include "PhoneNumber.h"

using namespace Utilities;

/////////////////////////////////////////////////////////////////////////////////////////
// Token generation

static char WaToken[] = "PdA2DJyKoUrwLw1Bg6EIhzh502dF9noR9uFCllGk1435688727801";

std::string WAToken::GenerateToken(const string &number)
{
	uint8_t digest[16];
	md5_string(WaToken + number, digest);

	char dest[33];
	bin2hex(digest, sizeof(digest), dest);
	return dest;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Account registration
CMStringA WARegister::RequestCodeUrl(const std::string &phoneNumber, const std::string &code)
{
	try {
		std::string id = GenerateIdentity(phoneNumber);

		PhoneNumber pn(phoneNumber);
		std::string token = WAToken::GenerateToken(pn.Number);

		const char *n = pn.Number.c_str();

		if (!code.empty() && code != "voice" && code != "sms")
			return CMStringA(FORMAT, "https://v.whatsapp.net/v2/register?cc=%d&in=%s&id=%s&code=%s", pn.countryCode, n, id.c_str(), code.c_str());

		return CMStringA(FORMAT, "https://v.whatsapp.net/v2/code?cc=%d&in=%s&to=%d%s&method=%s&mcc=%03d&mnc=%03d&token=%s&id=%s&lg=%s&lc=%s",
							pn.countryCode, n, pn.countryCode, n, code.c_str(), pn.mcc, pn.mnc, token.c_str(), id.c_str(), pn.ISO639, pn.ISO3166);
	}
	catch (...)
	{}
	
	return CMStringA();
}


std::string WARegister::GenerateIdentity(const std::string &phone)
{
	std::string id = phone;
	std::reverse(id.begin(), id.end());

	BYTE hash[MIR_SHA1_HASH_SIZE];
	mir_sha1_hash((PBYTE)id.c_str(), (int)id.length(), hash);
	
	id.clear();
	for (int i = 0; i < sizeof(hash); i++) {
		char buf[10];
		sprintf_s(buf, "%%%02x", hash[i]);
		id += buf;
	}

	return id;
}