压缩、网络

This commit is contained in:
yangfan 2024-04-25 10:55:09 +08:00
parent f689f1d74f
commit 9bc7f6cf7a
6 changed files with 197 additions and 1 deletions

View File

@ -8,6 +8,8 @@
"version": "1.0.0",
"dynamicDependencies": {},
"dependencies": {
"libcomb.so": "file:./src/main/cpp/types/libcomb"
"libcomb.so": "file:./src/main/cpp/types/libcomb",
"@ohos/axios": "^2.0.0",
"@ohos/commons-compress": "2.0.1"
}
}

View File

@ -66,6 +66,12 @@ export interface IConfigFileRes {
etag: string
}
export interface IConfigFileRes1 {
data: Uint8Array
etag: string
}
export interface ICombUpdater {
update: (isDiff: boolean) => Promise<void>
getVer: () => Promise<IVerData>

View File

@ -0,0 +1,65 @@
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
export class CipherUtil {
private static ALGORITHM = "AES";
private static MODE = "|CBC|PKCS5";
public static async generateKey(bit: number, str?: string): Promise<cryptoFramework.SymKey> {
const algorithm = CipherUtil.ALGORITHM + bit
const generator = cryptoFramework.createSymKeyGenerator(algorithm)
if (str) {
// 给定的字节字符串
const textEncoder = new util.TextEncoder()
// 将字节字符串转换为字节数组
const key = await generator.convertKey({ data: textEncoder.encodeInto(str) })
return key
} else {
const key = await generator.generateSymKey()
return key
}
}
public static async encrypt(data: string, key: cryptoFramework.Key, bit: number = 256) {
const zeroIv = CipherUtil.genIvParamsSpec();
const mode = CipherUtil.ALGORITHM + bit + CipherUtil.MODE
const cipher = cryptoFramework.createCipher(mode)
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, key, zeroIv)
const output = await cipher.doFinal({ data: CipherUtil.stringToUint8Array(data) })
return output
}
public static async decrypt(data: Uint8Array, key: cryptoFramework.Key, bit: number = 256) {
try {
const zeroIv = CipherUtil.genIvParamsSpec();
const mode = CipherUtil.ALGORITHM + bit + CipherUtil.MODE
const cipher = cryptoFramework.createCipher(mode)
await cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, key, zeroIv)
const output = await cipher.doFinal({ data:data })
// const tag = await cipher.doFinal(null)
return output
} catch (err) {
throw new Error('decrypt error')
}
}
// 生成IvParamsSpec
private static genIvParamsSpec() {
const textEncoder = new util.TextEncoder()
let ivBlob: cryptoFramework.DataBlob = { data: textEncoder.encodeInto('aacbcb1b3299ff61') };
let ivParamsSpec: cryptoFramework.IvParamsSpec = {
algName: "IvParamsSpec",
iv: ivBlob
};
return ivParamsSpec;
}
public static stringToUint8Array(str: string) {
let arr = new Array<number>()
for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
return new Uint8Array(arr)
}
}

View File

@ -0,0 +1,68 @@
import axios, { AxiosResponse } from '@ohos/axios';
import { IConfigFileRes, IConfigFileRes1, IVerData } from '../comb_type/CombTypes';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { util } from '@kit.ArkTS';
import { CipherUtil } from './CipherUtil';
import { CompressUtil } from './CompressUtil';
export class CombNetUtil {
public static async getVer(projectId: number, currVer: number, skv: number): Promise<IVerData> {
const url: string = `honeycomb.wps.cn/api/v4/project/${projectId}/ver`
const headers: Record<string, string> = {
'Client-Type': 'wps-harmony',
'Client-Ver': '1',
'Client-Chann': '1'
}
const params: Record<string, string> = {
'ver': String(currVer),
'skv': String(skv)
}
const res: AxiosResponse = await CombNetUtil.get(url, headers, params)
if (res.status === 200 && res.data.data) {
return res.data.data
} else {
throw new Error('getVer error')
}
}
public static async getConfigFile(url: string): Promise<IConfigFileRes1> {
const headers: Record<string, string> = {
'Accept-Encoding': 'br;q=1.0, gzip;q=0.8'
}
const res: AxiosResponse<ArrayBuffer> = await axios.request({
url: url,
headers,
method: 'get'
})
const data = new Uint8Array(res.data)
if (res.status === 200) {
const etag = (res.headers['etag'] ?? '') as string
return {
data: data,
etag: etag.replace(/^"|"$/g, '')
}
} else {
throw new Error('getConfigFile error')
}
}
public static async getIds() {
}
private static async get(url: string, headers?: Record<string, string>, params?: Record<string, string>) {
const header: Record<string, string> = headers ?? {}
const param: Record<string, string> = params ?? {}
const axiosResponse: AxiosResponse = await axios.request({
url: url,
method: 'get',
headers: header,
params: param
})
return axiosResponse
}
private static post() {
}
}

View File

@ -0,0 +1,50 @@
import fs from '@ohos.file.fs';
import {
CompressorInputStream,
CompressorStreamFactory,
File,
InputStream,
IOUtils,
OutputStream
} from '@ohos/commons-compress';
export class CompressUtil {
public static unBrotli(a: Uint8Array) {
try {
const data = getContext().tempDir + '/compress'
CompressUtil.generateTextFile(data, '/bla.tar.br', a)
let input = new File(data, "/bla.tar.br");
let output = new File(data, "/bla.tar");
let out: OutputStream = new OutputStream();
let input1: InputStream = new InputStream();
out.setFilePath(output.getPath());
input1.setFilePath(input.getPath());
let inputs: CompressorInputStream = new CompressorStreamFactory().createCompressorNameInputStream("br", input1)
IOUtils.copyStream(inputs, out);
out.close();
input1.close();
const deCompressRes = CompressUtil.getTextContent(output.getPath())
return deCompressRes
} catch (error) {
console.error('File to obtain the file directory. Cause: ' + error.message);
throw new Error('File to unBrotli' + error.message)
}
}
static generateTextFile(srcPath: string, fileName: string, arr: Uint8Array): void {
try {
if (!fs.accessSync(srcPath)) {
fs.mkdirSync(srcPath);
}
const writer = fs.openSync(srcPath + fileName, 0o102);
fs.writeSync(writer.fd, arr.buffer);
fs.closeSync(writer);
} catch (err) {
}
}
static getTextContent(path: string): string {
return fs.readTextSync(path)
}
}

View File

@ -6,6 +6,11 @@
"default",
"tablet",
"2in1"
],
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
}
]
}
}