因為沒有專案在使用這門語言,所以就沒有進一步的了解據聞公司明年的合作伙伴採用Dart來作為專案的主要撰寫語言,
所以再度把AzDG拿來練手,和google 另一門語言 Go 一樣還滿易學的。
import 'package:crypto/crypto.dart' show MD5, CryptoUtils;
import 'package:utf/utf.dart';
class AzDG {
String _cipher = 'Private key';
List< int > _cipherHash;
AzDG({String cipher}) {
if (cipher != null) {
_cipher = cipher;
}
var md5 = new MD5();
md5.add(this._cipher.codeUnits);
_cipherHash = CryptoUtils.bytesToHex(md5.close()).codeUnits;
}
List < int > _cipherEncode(List < int > inputData) {
var outData = new List < int > (inputData.length);
var loopCount = inputData.length;
for (var i = 0; i & lt; loopCount; i++) {
outData[i] = inputData[i] ^ _cipherHash[i % 32];
}
return outData;
}
String Decrypt(String sourceText) {
var decodeSourceText = _cipherEncode(CryptoUtils.base64StringToBytes(sourceText));
var size = (decodeSourceText.length~ / 2).toInt();
var outData = new List < int > (size);
for (int j = 0; j < size; ++j) {
outData[j] = (decodeSourceText[(j * 2)] ^ decodeSourceText[(j * 2) + 1]);
}
return decodeUtf8(outData);
}
String Crypt(String sourceText) {
var now = new DateTime.now();
var md5 = new MD5();
md5.add(now.toString().codeUnits);
var noise = CryptoUtils.bytesToHex(md5.close()).codeUnits;
var inputData = encodeUtf8(sourceText);
var outData = new List < int > (inputData.length * 2);
for (var j = 0; j < inputData.length; j++) {
outData[(j * 2)] = noise[j % 32];
outData[(j * 2) + 1] = inputData[j] ^ noise[j % 32];
}
return CryptoUtils.bytesToBase64(_cipherEncode(outData));
}
}
沒有留言:
張貼留言