There is such a function in Python:
def getToken(self, phoneNumber): keyDecoded = bytearray(base64.b64decode(self.__class__._KEY)) sigDecoded = base64.b64decode(self.__class__._SIGNATURE) clsDecoded = base64.b64decode(self.__class__._MD5_CLASSES) data = sigDecoded + clsDecoded + phoneNumber.encode() opad = bytearray() ipad = bytearray() for i in range(0, 64): opad.append(0x5C ^ keyDecoded[i]) ipad.append(0x36 ^ keyDecoded[i]) hash = hashlib.sha1() subHash = hashlib.sha1() try: subHash.update(ipad + data) hash.update(opad + subHash.digest()) except TypeError: subHash.update(bytes(ipad + data)) hash.update(bytes(opad + subHash.digest())) result = base64.b64encode(hash.digest()) return result Translated into the following C # code:
public string GetToken(string number) { byte[] key = Convert.FromBase64String(KEY); string signature = Convert.ToString(Convert.FromBase64String(SIGNATURE)); string md5Classes = Convert.ToString(Convert.FromBase64String(MD5_CLASSES)); string data = signature + md5Classes + number; List <byte> opad = new List<byte>(); List<byte> ipad = new List<byte>(); for(int i = 0; i < 64; i++) { opad.Add(Convert.ToByte(0x5C ^ key[i])); ipad.Add(Convert.ToByte(0x36 ^ key[i])); } SHA1 hash = SHA1.Create(); byte[] btData = Encoding.Default.GetBytes(data); ipad.AddRange(btData); byte[] temp = hash.ComputeHash(ipad.ToArray()); opad.AddRange(temp); temp = hash.ComputeHash(opad.ToArray()); return Convert.ToBase64String(temp); } But the values returned are different. Tell me where is the error?
numberin the C # version? - Grundybase64.b64decodefunction returns a string using some encoding. On Sharpe, theConvert.FromBase64Stringfunction returns an array of bytes — and in order to get a string, you need to know the encoding that the function from Python implies. You "at random" putConvert.ToString- and, apparently, were mistaken. The same can be said about other places with the conversion between a sequence of bytes and a string. - Pavel Mayorov