Les meilleurs développeurs Node.js freelances sont sur Codeur.com

Portage d'une fonction de C# vers NodeJS

 Terminé·30 à 300 €·2 offres·1021 vues


Bonjour,

J'aurais besoin de porter une fonction de C# vers NodeJS. La fonction est courte (30 lignes), prend en paramètre une clé et un timestamp et calcule un code, à l'aide de masques de bits et d'un hash SHA1.

Ci-dessous le code:

public string GenerateCodeForTime(string secretKey, long time)
{
byte[] CodeTranslations = new byte[] { 50, 51, 52, 53, 54, 55, 56, 57, 66, 67, 68, 70, 71, 72, 74, 75, 77, 78, 80, 81, 82, 84, 86, 87, 88, 89 };

byte[] sharedSecretArray = Convert.FromBase64String(this.secretKey);

byte[] timeArray = new byte[8];

time /= 30L;

for (int i = 8; i > 0; i--)
{
timeArray[i - 1] = (byte)time;
time >>= 8;
}

HMACSHA1 hmacGenerator = new HMACSHA1();
hmacGenerator.Key = sharedSecretArray;
byte[] hashedData = hmacGenerator.ComputeHash(timeArray);
byte[] codeArray = new byte[5];
try
{
byte b = (byte)(hashedData[19] & 0xF);
int codePoint = (hashedData[b] & 0x7F) << 24 | (hashedData[b + 1] & 0xFF) << 16 | (hashedData[b + 2] & 0xFF) << 8 | (hashedData[b + 3] & 0xFF);

for (int i = 0; i < 5; ++i)
{
codeArray[i] = CodeTranslations[codePoint % CodeTranslations.Length];
codePoint /= CodeTranslations.Length;
}
}
catch (Exception e)
{
return null;
}
return Encoding.UTF8.GetString(codeArray);
}

Exemple de résultats à reproduire:
GenerateCode("6HIo5Sp3Zy/yuw5zkGq2llg9rMY=", [Téléphone visible pour les membres Pro]) = "K3PPD"
GenerateCode("6HIo5Sp3Zy/yuw5zkGq2llg9rMY=", [Téléphone visible pour les membres Pro]) = "BC3VM"
GenerateCode("6HIo5Sp3Zy/yuw5zkGq2llg9rMY=", [Téléphone visible pour les membres Pro]) = "BC3VM"
GenerateCode("6HIo5Sp3Zy/yuw5zkGq2llg9rMY=", [Téléphone visible pour les membres Pro]) = "D2VJG"

GenerateCode.zip

Budget indicatif : 30 à 300 €

Publication : 30 novembre 2015 à 16h33

Profils recherchés : Développeur Node.js freelance

Le profil du client est reservé aux prestataires abonnés

Créer un compte

2 freelances ont répondu à ce projet

2 propositions de devis en moins de 2h

Publier un projet similaire

Projet réalisé par jcaron

jcaron
London, W1S 1DX

Chaque jour, des centaines de clients utilisent Codeur.com pour trouver un prestataire. Créez votre compte dès maintenant, remplissez votre profil et trouvez de nouveaux clients.

Trouver des nouveaux clients

Votre navigateur Web n’est plus à jour. Il ne permet pas d’afficher correctement le site Codeur.com.
Nous vous invitons à mettre à jour votre navigateur ou à utiliser un autre navigateur plus récent.