Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.02 KB | None | 0 0
  1. import 'package:crypto/crypto.dart';
  2. import 'dart:convert';
  3.  
  4. class Sha1Encryptor {
  5.   /// This method will change the List<int> into a Big Int.
  6.   /// I did not write this myself
  7.   ///
  8.   /// Source: https://github.com/PointyCastle/pointycastle/blob/master/lib/src/utils.dart
  9.   BigInt _decodeBigInt(List<int> bytes) {
  10.     var result = BigInt.from(0);
  11.     for (var i = 0; i < bytes.length; i++) {
  12.       result += BigInt.from(bytes[bytes.length - i - 1]) << (8 * i);
  13.     }
  14.     return result;
  15.   }
  16.  
  17.   String _removeFirstZeros(String str) {
  18.     var split = str.split('');
  19.  
  20.     for (var i = 0; i < split.length; i++) {
  21.       if (split[i] == '0') {
  22.         str = str.replaceFirst('0', '');
  23.       } else {
  24.         if(i >1){
  25.           print('wtf : ' + i.toString());
  26.         }
  27.         break;
  28.       }
  29.     }
  30.     return str;
  31.   }
  32.  
  33.   String getEncrypted(String str) {
  34.     //First we convert the string into bytes (UTF-8) and then encrypt them with the SHA-1 algrorithm.
  35.     var digest = sha1.convert(utf8.encode(str));
  36.  
  37.     //If the first byte is smaller than 128, it should be seen as a negative number.
  38.     if (digest.bytes[0] < 128) {
  39.       return _removeFirstZeros(digest.toString());
  40.     }
  41.  
  42.     //We turn the result from the encryption into a big int.
  43.     var bytesAsBigInt = _decodeBigInt(digest.bytes);
  44.  
  45.     //As we want to perform two's complement, we first have to turn the big int into bits and then changing
  46.     //all the 0's to 1's and the other way around.
  47.     var bytesReversed = bytesAsBigInt
  48.         .toRadixString(2)
  49.         .replaceAll('0', '2')
  50.         .replaceAll('1', '0')
  51.         .replaceAll('2', '1');
  52.  
  53.     //Since we need to add 1 to the bit series (two's complement), we will split each bit from eachother into a List.
  54.     var split = bytesReversed.split('');
  55.  
  56.     //We will loop through every bit in the list to make sure we correctly add the bit.
  57.     //The is a very small chance of carry overloading, but the chance is too small to care about it.
  58.     //A better developer would've looked into that.
  59.     var addedOne = '';
  60.     var carry = true;
  61.     for (var i = split.length - 1; i >= 0; i--) {
  62.       var char = split[i];
  63.  
  64.       if (carry) {
  65.         if (char == '0') {
  66.           char = '1';
  67.           carry = false;
  68.         } else {
  69.           //Keep the carry
  70.           char = '0';
  71.         }
  72.       }
  73.       addedOne = (char + addedOne);
  74.     }
  75.  
  76. //Return the bit series as HEX values
  77.     return '-' +
  78.         _removeFirstZeros(BigInt.parse(addedOne, radix: 2).toRadixString(16));
  79.   }
  80.  
  81.     String getServerHash(String serverID, List PublicKey, List SecretKey)
  82.         {
  83.          
  84.  
  85.  
  86.             byte[] hash = digest(new byte[][] {  Encoding.GetEncoding("iso-8859-1").GetBytes(serverID), SecretKey, PublicKey });
  87.             bool negative = (hash[0] & 0x80) == 0x80;
  88.             if (negative) { hash = TwosComplementLittleEndian(hash); }
  89.             string result = GetHexString(hash).TrimStart('0');
  90.             if (negative) { result = "-" + result; }
  91.             return result;
  92.         }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement