Develop crypto library

This commit is contained in:
Vladimir Privezenov
2025-12-17 19:27:43 +03:00
parent 2138e38d00
commit d6490a1671
14 changed files with 1641 additions and 254 deletions

View File

@ -1,185 +0,0 @@
import browser from "webextension-polyfill";
import {KeyPair} from "./keys/keys.ts";
import {selectUserJSON} from "./utils.ts";
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
global["AscCommon"] = global["AscCommon"] || {};
global["AscCommon"].KeyStorageLibrary = factory();
}
}(typeof window !== 'undefined' ? window : global, function() {
function StorageManager() {
}
StorageManager.prototype.getStorageKeys = function() {
return new Promise(function(resolve) {
resolve([]);
});
};
StorageManager.prototype.setStorageKeys = function(jsonKeys) {
return new Promise(function(resolve) {
resolve([]);
});
};
StorageManager.prototype.getMasterPassword = function() {
return new Promise(function(resolve) {
resolve(null);
});
};
StorageManager.prototype.setStorageKeys = function() {
return new Promise(function(resolve) {
resolve(null);
});
};
StorageManager.prototype.setMasterPasswordWithKeys = function() {
return new Promise(function(resolve) {
resolve(null);
});
};
function KeyStorage(storageManager) {
this.keys = [];
this.storageManager = storageManager;
}
KeyStorage.prototype.loadKeysFromStorage = function() {
const oThis = this;
return this.storageManager.getStorageKeys().then(function(keys) {
return oThis.loadKeys(keys);
}).then(function(loadedKeys) {
oThis.keys = loadedKeys;
});
};
KeyStorage.prototype.loadKeys = function(exportedKeys) {
return this.getMasterPassword().then(function(masterPassword) {
if (masterPassword === null) {
return [];
}
const promises = exportedKeys.map(function(key) {
return KeyPair.fromJSON(key, masterPassword);
});
return Promise.all(promises);
});
};
KeyStorage.prototype.addNewKeys = function(keys) {
this.keys.push.apply(this.keys, keys);
return this.writeKeys();
};
KeyStorage.prototype.export = function() {
const oThis = this;
return this.getMasterPassword().then(function(masterPassword) {
if (typeof masterPassword !== "string") {
return [];
}
const exportedKeyPromise = oThis.keys.map(function(key) {
return key.export(masterPassword);
});
return Promise.all(exportedKeyPromise);
});
};
KeyStorage.prototype.writeKeys = function() {
const oThis = this;
return this.export().then(function(exportedKeys) {
oThis.setStorageKeys(exportedKeys);
});
};
KeyStorage.prototype.setStorageKeys = function(exportedKeys) {
return this.storageManager.setStorageKeys(exportedKeys);
};
KeyStorage.prototype.getMasterPassword = function() {
return this.storageManager.getMasterPassword();
};
KeyStorage.prototype.changeMasterPassword = function(newMasterPassword) {
const oThis = this;
return this.export(newMasterPassword).then(function(keys) {
return oThis.storageManager.setMasterPasswordWithKeys(newMasterPassword, keys);
});
};
KeyStorage.prototype.exportKeys = function() {
return this.export().then(function(data) {
const passwordInfo = {
encrypt: false,
data: data
};
const blob = new Blob([JSON.stringify(passwordInfo)], { type: "application/json"});
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `onlyoffice_keychain_${(new Date()).toISOString()}.json`;
link.click();
URL.revokeObjectURL(url);
});
};
KeyStorage.prototype.importKeys = function(callback) {
const oThis = this;
selectUserJSON(function (file) {
try {
file.text().then(function(text) {
const json = JSON.parse(text);
if (!json.encrypt) {
return oThis.loadKeys(json.data);
} else {
throw "Need support encrypt JSON"
}
}).then(function(keyObjects) {
return oThis.addNewKeys(keyObjects);
}).then(function() {
callback();
});
} catch (e) {
}
});
};
KeyStorage.prototype.getKeyPairByGuid = function(guid) {
for (let i = 0; i < this.keys.length; i++) {
const key = this.keys[i];
if (guid === key.guid) {
return key;
}
}
};
KeyStorage.prototype.getValidKeys = function() {
const keys = [];
for (let i = 0; i < this.keys.length; i++) {
const key = this.keys[i];
if (key.isValid) {
keys.push(key);
}
}
return keys;
};
KeyStorage.prototype.deprecateKey = function(key) {
key.setIsValid(false);
return this.writeKeys();
};
KeyStorage.prototype.generateKey = function (keyParams) {
const crypto = getCrypto();
return crypto.generateKey(keyParams);
};
return {
KeyStorage: KeyStorage,
StorageManager: StorageManager
};
}));

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
{
"devDependencies": {
"vite": "^7.3.0"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}

View File

@ -1,10 +1,10 @@
import {digestTypes, exportKeyFormats,} from "./keys/key-types.ts";
import {AesGcmGenParams} from "./keys/params.ts";
import {WebEncryptKeyPair, WebSignKeyPair, WebSymmetricKey} from "./keys";
import {AesGcmGenParams} from "./params";
import {c_oAscDigestType, c_oAscExportKeyFormat} from "./defines";
const pbkdf2Parameters = {
iterations: 150000,
hash: digestTypes.SHA256,
hash: c_oAscDigestType.SHA256,
saltLength: 16
};
@ -59,12 +59,13 @@ CWebCrypto.prototype.getAesCryptoKey = function(masterPassword, salt) {
CWebCrypto.prototype.wrapKey = function (format, key, masterPassword, salt, aesParams, keyUsage) {
const oThis = this;
return Promise.all([this.getAesCryptoKey(masterPassword, salt), this.getCryptoKeyFromWrapper(key, keyUsage)]).then(function(cryptoKeys) {
const cryptoKey = key.getCryptoKey();
return Promise.all([this.getAesCryptoKey(masterPassword, salt), cryptoKey]).then(function(cryptoKeys) {
return oThis.subtle.wrapKey(format, cryptoKeys[1], cryptoKeys[0], aesParams);
});
}
CWebCrypto.prototype.unwrapKey = function(format, key, masterPassword, salt, aesParams, keyParams, keyUsages) {
CWebCrypto.prototype.unwrapKey = function(format, key, masterPassword, salt, aesParams, keyParams) {
const oThis = this;
return this.getAesCryptoKey(masterPassword, salt).then(function(cryptoAesKey) {
return oThis.subtle.unwrapKey(format, key, cryptoAesKey, aesParams, keyParams, true, /*this.getKeyUsages(keyUsages)*/["sign"]);
@ -72,9 +73,6 @@ CWebCrypto.prototype.unwrapKey = function(format, key, masterPassword, salt, aes
return oThis.subtle.exportKey(format, cryptoKey);
});
}
CWebCrypto.prototype.getCryptoKeyFromWrapper = function(key, keyUsages) {
return this.subtle.importKey(key.exportFormat, key.key, key.params, true, keyUsages);
}
CWebCrypto.prototype.sign = function(key, data) {
const oThis = this;
const cryptoKey = key.getCryptoKey();
@ -111,9 +109,9 @@ CWebCrypto.prototype.generateKey = function(params) {
const cryptoUsages = params.getCryptoUsages();
return this.subtle.generateKey(cryptoParams, true, cryptoUsages).then(function(cryptoKey) {
if (cryptoKey.privateKey && cryptoKey.publicKey) {
return Promise.all([oThis.subtle.exportKey(exportKeyFormats.spki, cryptoKey.publicKey), oThis.subtle.exportKey(exportKeyFormats.pkcs8, cryptoKey.privateKey)]);
return Promise.all([oThis.subtle.exportKey(c_oAscExportKeyFormat.spki, cryptoKey.publicKey), oThis.subtle.exportKey(c_oAscExportKeyFormat.pkcs8, cryptoKey.privateKey)]);
}
return oThis.subtle.exportKey(exportKeyFormats.raw, cryptoKey);
return oThis.subtle.exportKey(c_oAscExportKeyFormat.raw, cryptoKey);
}).then(function(exportedKeys) {
const importParams = params.getImportParams();
if (Array.isArray(exportedKeys)) {
@ -133,6 +131,6 @@ CWebCrypto.prototype.randomUUID = function() {
return this.crypto.randomUUID();
}
function getCrypto() {
export function getCrypto() {
return new CWebCrypto();
}

View File

@ -0,0 +1,28 @@
export const c_oAscAlgorithmType = {
AES_GCM : 1,
ED25519 : 2,
RSA_OAEP : 3
};
export const c_oAscDigestType = {
SHA1: 1,
SHA256: 2,
SHA384: 3,
SHA512: 4
};
export const c_oAscExportKeyFormat = {
pkcs8: "pkcs8",
spki: "spki",
raw: "raw"
};
export const c_oAscKeyStorageType = {
NoType: 0,
WebSymmetricKey: 1,
WebSignKeyPair: 2,
WebEncryptKeyPair: 3,
WebPublicKey: 4,
WebPrivateKey: 5,
Ed25519ImportParams: 6,
};

View File

@ -0,0 +1,9 @@
import {c_oAscKeyStorageType} from "./defines";
import {WebEncryptKeyPair, WebPrivateKey, WebPublicKey, WebSignKeyPair, WebSymmetricKey} from "./keys";
export const c_oAscObjectFactory = {};
c_oAscObjectFactory[c_oAscKeyStorageType.WebPublicKey] = WebPublicKey;
c_oAscObjectFactory[c_oAscKeyStorageType.WebPrivateKey] = WebPrivateKey;
c_oAscObjectFactory[c_oAscKeyStorageType.WebSymmetricKey] = WebSymmetricKey;
c_oAscObjectFactory[c_oAscKeyStorageType.WebSignKeyPair] = WebSignKeyPair;
c_oAscObjectFactory[c_oAscKeyStorageType.WebEncryptKeyPair] = WebEncryptKeyPair;

View File

@ -0,0 +1 @@
export {StorageManager, KeyStorage} from './key-storage';

View File

@ -0,0 +1,182 @@
import {getCrypto} from "./crypto";
import {BinaryReader, readObject} from "./serialize/reader";
import {BinaryWriter, writeObject} from "./serialize/writer";
export function StorageManager() {
}
StorageManager.prototype.getStorageKeys = function () {
return new Promise(function (resolve) {
resolve([]);
});
};
StorageManager.prototype.setStorageKeys = function (jsonKeys) {
return new Promise(function (resolve) {
resolve([]);
});
};
StorageManager.prototype.getMasterPassword = function () {
return new Promise(function (resolve) {
resolve(null);
});
};
StorageManager.prototype.setStorageKeys = function () {
return new Promise(function (resolve) {
resolve(null);
});
};
StorageManager.prototype.setMasterPasswordWithKeys = function () {
return new Promise(function (resolve) {
resolve(null);
});
};
export function KeyStorage(storageManager) {
this.keys = [];
this.storageManager = storageManager;
}
KeyStorage.prototype.loadKeysFromStorage = function () {
const oThis = this;
return this.storageManager.getStorageKeys().then(function (keys) {
return oThis.loadKeys(keys);
}).then(function (loadedKeys) {
oThis.keys = loadedKeys;
});
};
KeyStorage.prototype.loadKeys = function (exportedKeys) {
return this.getMasterPassword().then(function (masterPassword) {
if (masterPassword === null) {
return [];
}
const promises = exportedKeys.map(function (key) {
return KeyPair.fromJSON(key, masterPassword);
});
return Promise.all(promises);
});
};
KeyStorage.prototype.addNewKeys = function (keys) {
this.keys.push.apply(this.keys, keys);
return this.writeKeys();
};
KeyStorage.prototype.export = function () {
const oThis = this;
const writer = new BinaryWriter();
writer.WriteLong(this.keys.length);
for (let i = 0; i < this.keys.length; i++) {
const key = this.keys[i];
writeObject(writer, key);
}
return writer.GetData();
};
KeyStorage.prototype.import = function (binaryData) {
const oThis = this;
const reader = new BinaryReader(binaryData, binaryData.length);
const length = reader.GetLong();
for (let i = 0; i < length; i++) {
const key = readObject(reader);
if (key) {
this.keys.push(key);
}
}
};
KeyStorage.prototype.writeKeys = function () {
const oThis = this;
return this.export().then(function (exportedKeys) {
oThis.setStorageKeys(exportedKeys);
});
};
KeyStorage.prototype.setStorageKeys = function (exportedKeys) {
return this.storageManager.setStorageKeys(exportedKeys);
};
KeyStorage.prototype.getMasterPassword = function () {
return this.storageManager.getMasterPassword();
};
KeyStorage.prototype.changeMasterPassword = function (newMasterPassword) {
const oThis = this;
return this.export(newMasterPassword).then(function (keys) {
return oThis.storageManager.setMasterPasswordWithKeys(newMasterPassword, keys);
});
};
KeyStorage.prototype.exportKeys = function () {
return this.export().then(function (data) {
const passwordInfo = {
encrypt: false,
data : data
};
const blob = new Blob([JSON.stringify(passwordInfo)], {type: "application/json"});
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `onlyoffice_keychain_${(new Date()).toISOString()}.json`;
link.click();
URL.revokeObjectURL(url);
});
};
KeyStorage.prototype.importKeys = function (callback) {
const oThis = this;
selectUserJSON(function (file) {
try {
file.text().then(function (text) {
const json = JSON.parse(text);
if (!json.encrypt) {
return oThis.loadKeys(json.data);
} else {
throw "Need support encrypt JSON"
}
}).then(function (keyObjects) {
return oThis.addNewKeys(keyObjects);
}).then(function () {
callback();
});
} catch (e) {
}
});
};
KeyStorage.prototype.getKeyPairByGuid = function (guid) {
for (let i = 0; i < this.keys.length; i++) {
const key = this.keys[i];
if (guid === key.guid) {
return key;
}
}
};
KeyStorage.prototype.getValidKeys = function () {
const keys = [];
for (let i = 0; i < this.keys.length; i++) {
const key = this.keys[i];
if (key.isValid) {
keys.push(key);
}
}
return keys;
};
KeyStorage.prototype.deprecateKey = function (key) {
key.setIsValid(false);
return this.writeKeys();
};
KeyStorage.prototype.generateKey = function (keyParams) {
const crypto = getCrypto();
return crypto.generateKey(keyParams);
};

View File

@ -1,25 +1,3 @@
import getCrypto from "../crypto.ts";
function writeString(memory) {
}
function writeBool() {
}
function writeLong() {
}
function writeByte() {
}
function writeDouble() {
}
function writeBuffer() {
}
function writeObject() {
}
function PromiseManager(initPromise) {
this.data = null;
this.error = null;
@ -59,14 +37,7 @@ PromiseManager.prototype.handleResolvers = function() {
}
}
};
const c_oAscCipherKeyType = {
NoType: 0,
WebSymmetricKey: 1,
WebSignKeyPair: 2,
WebEncryptKeyPair: 3,
WebPublicKey: 4,
WebPrivateKey: 5,
};
function CryptoKeyBase() {
@ -75,7 +46,7 @@ function CryptoKeyBase() {
this.uid = null;
this.version = 1;
this.params = null;
this.type = c_oAscCipherKeyType.NoType;
this.type = c_oAscKeyStorageType.NoType;
}
CryptoKeyBase.import = function(reader, version, symmetricKey) {};
CryptoKeyBase.prototype.init = function() {
@ -176,20 +147,20 @@ WebKeyPair.fromCryptoBuffer = function(publicKeyBuffer, privateKeyBuffer, import
return keyPair;
};
function WebSignKeyPair() {
export function WebSignKeyPair() {
WebKeyPair.call(this);
this.type = c_oAscCipherKeyType.WebSignKeyPair;
this.type = c_oAscKeyStorageType.WebSignKeyPair;
}
WebSignKeyPair.prototype = Object.create(WebKeyPair.prototype);
WebSignKeyPair.prototype.constructor = WebSignKeyPair;
WebSignKeyPair.import = WebKeyPair.import;
WebSignKeyPair.fromCryptoBuffer = WebKeyPair.fromCryptoBuffer;
function WebEncryptKeyPair() {
export function WebEncryptKeyPair() {
WebKeyPair.call(this);
this.privateKey = null;
this.publicKey = null;
this.type = c_oAscCipherKeyType.WebEncryptKeyPair;
this.type = c_oAscKeyStorageType.WebEncryptKeyPair;
}
WebEncryptKeyPair.prototype = Object.create(WebKeyPair.prototype);
WebEncryptKeyPair.prototype.constructor = WebEncryptKeyPair;
@ -200,7 +171,7 @@ function AsymmetricKey() {
this.binaryKey = null;
this.cryptoKey = null;
this.version = 1;
this.type = c_oAscCipherKeyType.NoType;
this.type = c_oAscKeyStorageType.NoType;
}
AsymmetricKey.prototype.setBinaryKey = function(binaryKey) {
this.binaryKey = binaryKey;
@ -213,9 +184,9 @@ AsymmetricKey.prototype.setVersion = function(version) {
};
AsymmetricKey.prototype.changeMasterPassword = function(oldMasterPassword, newMasterPassword) {};
function WebPrivateKey() {
export function WebPrivateKey() {
AsymmetricKey.call(this);
this.type = c_oAscCipherKeyType.WebPrivateKey;
this.type = c_oAscKeyStorageType.WebPrivateKey;
this.salt = null;
}
WebPrivateKey.prototype = Object.create(AsymmetricKey.prototype);
@ -259,7 +230,7 @@ WebPrivateKey.prototype.changeMasterPassword = function(oldMasterPassword, newMa
function WebPublicKey() {
export function WebPublicKey() {
AsymmetricKey.call(this);
this.binaryKey = null;
this.cryptoKey = null;
@ -292,10 +263,10 @@ WebPublicKey.prototype.export = function(writer) {
}
};
function WebSymmetricKey() {
export function WebSymmetricKey() {
CryptoKeyBase.call(this);
this.version = 1;
this.type = c_oAscCipherKeyType.WebSymmetricKey;
this.type = c_oAscKeyStorageType.WebSymmetricKey;
this.cryptoKey = null;
this.binaryKey = null;
}

View File

@ -1,14 +1,7 @@
const algorithmTypes = {
AES_GCM : 1,
ED25519 : 2,
RSA_OAEP : 3
};
export const digestTypes = {
SHA1: 1,
SHA256: 2,
SHA384: 3,
SHA512: 4,
};
import {c_oAscAlgorithmType} from "./defines";
import {readLong, readBuffer} from "./serialize/reader";
import {writeLong} from "./serialize/writer";
function AlgorithmParams(type) {
this.type = type;
this.version = 1;
@ -29,7 +22,7 @@ AlgorithmParams.import = function(reader) {
AlgorithmParams.prototype.setVersion = function (version) {
this.version = version;
};
AlgorithmParams.prototype.export = function() {
AlgorithmParams.prototype.export = function(writer) {
writeLong(writer, this.version);
switch (this.version) {
case 1: {
@ -90,7 +83,7 @@ RSAKeyGenParams.prototype.getImportParams = function() {
};
function Ed25519ImportParams() {
AlgorithmParams.call(this, algorithmTypes.ED25519);
AlgorithmParams.call(this, c_oAscAlgorithmType.ED25519);
}
Ed25519ImportParams.prototype = Object.create(AlgorithmParams.prototype);
Ed25519ImportParams.prototype.constructor = Ed25519ImportParams;
@ -106,7 +99,7 @@ Ed25519KeyGenParams.prototype.getImportParams = function() {
};
function AesGcmCryptoParams(iv, tagLength) {
AlgorithmParams.call(this, algorithmTypes.AES_GCM);
AlgorithmParams.call(this, c_oAscAlgorithmType.AES_GCM);
this.iv = iv || null;
this.tagLength = typeof tagLength === "number" ? tagLength : null;
}
@ -163,8 +156,8 @@ AesKeyGenParams.prototype.getImportParams = function() {
return new AlgorithmParams(this.name);
};
function AesGcmGenParams() {
AesKeyGenParams.call(this, algorithmTypes.AES_GCM, 256);
export function AesGcmGenParams() {
AesKeyGenParams.call(this, c_oAscAlgorithmType.AES_GCM, 256);
}
AesGcmGenParams.prototype = Object.create(AlgorithmParams.prototype);
AesGcmGenParams.prototype.constructor = AesGcmGenParams;

View File

@ -0,0 +1,133 @@
import {c_oAscObjectFactory} from "../factory";
var c_oSerConstants = {
ErrorFormat: -2,
ErrorUnknown: -1,
ReadOk:0,
ReadUnknown:1,
ErrorStream:0x55
};
export function BinaryReader(data, size) {
this.data = data;
this.size = size;
this.pos = 0;
this.cur = 0;
}
BinaryReader.prototype.Seek = function(_pos) {
if (_pos > this.size)
return c_oSerConstants.ErrorStream;
this.pos = _pos;
return c_oSerConstants.ReadOk;
};
BinaryReader.prototype.Seek2 = function(_cur) {
if (_cur > this.size)
return c_oSerConstants.ErrorStream;
this.cur = _cur;
return c_oSerConstants.ReadOk;
};
BinaryReader.prototype.Skip = function(_skip) {
if (_skip < 0)
return c_oSerConstants.ErrorStream;
return this.Seek(this.pos + _skip);
};
BinaryReader.prototype.Skip2 = function(_skip) {
if (_skip < 0)
return c_oSerConstants.ErrorStream;
return this.Seek2(this.cur + _skip);
};
BinaryReader.prototype.SkipRecord = function() {
var _len = this.GetLong();
this.Skip2(_len);
};
BinaryReader.prototype.GetUChar = function() {
if (this.cur >= this.size)
return 0;
return this.data[this.cur++];
};
BinaryReader.prototype.GetBool = function() {
var Value = this.GetUChar();
return ( Value == 0 ? false : true );
};
BinaryReader.prototype.GetLong = function() {
if (this.cur + 3 >= this.size)
return 0;
return (this.data[this.cur++] | this.data[this.cur++] << 8 | this.data[this.cur++] << 16 | this.data[this.cur++] << 24);
};
BinaryReader.prototype.GetString = function() {
var Len = this.GetLong();
if (this.cur + 2 * Len > this.size)
return "";
var t = "";
for (var i = 0; i + 1 < 2 * Len; i+=2) {
var uni = this.data[this.cur + i];
uni |= this.data[this.cur + i + 1] << 8;
t += String.fromCharCode(uni);
}
this.cur += 2 * Len;
return t;
};
BinaryReader.prototype.GetCurPos = function() {
return this.cur;
};
BinaryReader.prototype.GetSize = function() {
return this.size;
};
BinaryReader.prototype.GetDouble = function() {
var dRes = 0.0;
dRes |= this.GetUChar();
dRes |= this.GetUChar() << 8;
dRes |= this.GetUChar() << 16;
dRes |= this.GetUChar() << 24;
dRes /= 100000;
return dRes;
};
BinaryReader.prototype.GetBuffer = function(length) {
var res = new Uint8Array(length);
for(var i = 0 ; i < length ;++i){
res[i] = this.data[this.cur++]
}
return res;
};
export function readString(reader) {
const isWrite = reader.GetBool();
return isWrite ? reader.GetString() : null;
}
export function readBool(reader) {
const isWrite = reader.GetBool();
return isWrite ? reader.GetBool() : null;
}
export function readLong(reader) {
const isWrite = reader.GetBool();
return isWrite ? reader.GetLong() : null;
}
export function readBuffer(reader) {
const isWrite = reader.GetBool();
return isWrite ? reader.GetBuffer(reader.GetLong()) : null;
}
export function readObject(reader) {
const isWrite = reader.GetBool();
if (isWrite) {
const type = reader.GetUChar();
var nStart = reader.cur;
var nEnd = nStart + reader.GetLong() + 4;
if (c_oAscObjectFactory[type]) {
const object = new c_oAscObjectFactory[type];
object.import(reader);
return object;
}
reader.Seek2(nEnd);
}
return null;
}

View File

@ -0,0 +1,173 @@
import {c_oAscObjectFactory} from "../factory";
export function BinaryWriter(bIsNoInit)
{
this.data = null;
this.len = 0;
this.pos = 0;
if (true !== bIsNoInit)
this.Init();
}
BinaryWriter.prototype.Init = function(len)
{
this.len = (len === undefined) ? 1024 * 1024 * 5 : len;
this.data = new Uint8Array(this.len);
this.pos = 0;
}
BinaryWriter.prototype.CheckSize = function(count)
{
if (this.pos + count >= this.len)
{
var oldData = this.data;
this.len = Math.max(this.len * 2, this.pos + ((3 * count / 2) >> 0));
this.data = new Uint8Array(this.len);
for (var i = 0; i < this.pos; i++)
this.data[i] = oldData[i];
}
}
BinaryWriter.prototype.GetData = function()
{
var len = this.GetCurPosition();
var res = new Uint8Array(len);
for (var i = 0; i < len; i++)
res[i] = this.data[i];
return res;
}
BinaryWriter.prototype.GetCurPosition = function()
{
return this.pos;
}
BinaryWriter.prototype.Seek = function(nPos)
{
this.pos = nPos;
}
BinaryWriter.prototype.Skip = function(nDif)
{
this.pos += nDif;
}
BinaryWriter.prototype.WriteBool = function(val)
{
this.CheckSize(1);
if (false == val)
this.data[this.pos++] = 0;
else
this.data[this.pos++] = 1;
}
BinaryWriter.prototype.WriteByte = function(val)
{
this.CheckSize(1);
this.data[this.pos++] = val;
}
BinaryWriter.prototype.WriteLong = function(val)
{
this.CheckSize(4);
this.data[this.pos++] = (val) & 0xFF;
this.data[this.pos++] = (val >>> 8) & 0xFF;
this.data[this.pos++] = (val >>> 16) & 0xFF;
this.data[this.pos++] = (val >>> 24) & 0xFF;
}
BinaryWriter.prototype.WriteDouble = function(val)
{
this.CheckSize(4);
var lval = ((val * 100000) >> 0) & 0xFFFFFFFF;
this.data[this.pos++] = (lval) & 0xFF;
this.data[this.pos++] = (lval >>> 8) & 0xFF;
this.data[this.pos++] = (lval >>> 16) & 0xFF;
this.data[this.pos++] = (lval >>> 24) & 0xFF;
}
BinaryWriter.prototype.WriteString = function(text)
{
if ("string" != typeof text)
text = text + "";
var count = text.length & 0xFFFF;
this.CheckSize(2 * count + 2);
this.data[this.pos++] = count & 0xFF;
this.data[this.pos++] = (count >>> 8) & 0xFF;
for (var i = 0; i < count; i++)
{
var c = text.charCodeAt(i) & 0xFFFF;
this.data[this.pos++] = c & 0xFF;
this.data[this.pos++] = (c >>> 8) & 0xFF;
}
};
BinaryWriter.prototype.WriteBuffer = function(data, _pos, count)
{
this.CheckSize(count);
for (var i = 0; i < count; i++)
{
this.data[this.pos++] = data[_pos + i];
}
}
BinaryWriter.prototype.WriteItem = function(type, fWrite)
{
this.WriteByte(type);
this.WriteItemWithLength(fWrite);
};
BinaryWriter.prototype.WriteItemWithLength = function(fWrite)
{
var nStart = this.WriteItemWithLengthStart();
fWrite();
this.WriteItemWithLengthEnd(nStart);
};
BinaryWriter.prototype.WriteItemWithLengthStart = function()
{
var nStart = this.GetCurPosition();
this.Skip(4);
return nStart;
};
BinaryWriter.prototype.WriteItemWithLengthEnd = function(nStart)
{
var nEnd = this.GetCurPosition();
this.Seek(nStart);
this.WriteLong(nEnd - nStart - 4);
this.Seek(nEnd);
};
export function writeString(writer, str) {
const isWrite = typeof str === "string";
writer.WriteBool(isWrite);
if (isWrite) {
writer.WriteString(str);
}
}
export function writeBool(writer, bool) {
const isWrite = typeof bool === "boolean";
writer.WriteBool(isWrite);
if (isWrite) {
writer.WriteBool(bool);
}
}
export function writeLong(writer, int) {
const isWrite = typeof int === "number";
writer.WriteBool(isWrite);
if (isWrite) {
writer.WriteLong(int);
}
}
export function writeBuffer(writer, buffer) {
const isWrite = buffer instanceof Uint8Array;
writer.WriteBool(isWrite);
if (isWrite) {
writer.WriteLong(buffer.length);
writer.WriteBuffer(buffer, 0, buffer.length);
}
}
export function writeObject(writer, object) {
let isWrite = false;
const type = object && object.getType();
if (c_oAscObjectFactory[type]) {
isWrite = true;
}
writer.WriteBool(isWrite);
if (isWrite) {
writer.WriteItem(type, function () {
object.export(writer);
});
}
}

View File

@ -0,0 +1,18 @@
import { defineConfig } from 'vite'
import { resolve } from 'path'
export default defineConfig({
build: {
lib: {
formats: ['iife', 'es'],
entry: resolve(__dirname, 'src/index.js'),
name: 'KeyStorage',
fileName: (format) => `key-storage.${format}.js`
},
rollupOptions: {
output: {
generatedCode: "es5"
}
}
},
})