為屏蔽底層硬件和算法庫,向上提供統(tǒng)一的密碼算法庫加解密相關(guān)接口。
本模塊首批接口從API version 9開始支持。
表示執(zhí)行結(jié)果的枚舉。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 值 | 說明 |
---|---|---|
INVALID_PARAMS | 401 | 非法入?yún)ⅰ?/p> |
NOT_SUPPORT | 801 | 操作不支持。 |
ERR_OUT_OF_MEMORY | 17620001 | 內(nèi)存錯(cuò)誤。 |
ERR_RUNTIME_ERROR | 17620002 | 運(yùn)行時(shí)外部錯(cuò)誤。 |
ERR_CRYPTO_OPERATION | 17630001 | 調(diào)用三方算法庫API出錯(cuò)。 |
buffer數(shù)組。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
data | Uint8Array | 是 | 是 | 數(shù)據(jù)。 |
createMac(algName : string) : Mac
生成Mac實(shí)例,用于進(jìn)行消息認(rèn)證碼的計(jì)算與操作。
支持的規(guī)格詳見框架概述“HMAC消息認(rèn)證碼算法規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 指定摘要算法,支持算法請參考“HMAC算法支持范圍”一節(jié) |
返回值:
類型 | 說明 |
---|---|
Mac | 返回由輸入算法指定生成的Mac對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- // 參數(shù)選擇請參考上述算法支持范圍
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
Mac類,調(diào)用Mac方法可以進(jìn)行MAC(Message Authentication Code)加密計(jì)算。調(diào)用前,需要通過createMac構(gòu)造Mac實(shí)例。
init(key : SymKey, callback : AsyncCallback<void>) : void;
使用對稱密鑰初始化Mac計(jì)算
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | 是 | 共享對稱密鑰 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
- var KeyBlob;
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- }
- mac.init(symKey, (err1, ) => {
- if (err1) {
- console.error("[Callback] err: " + err1.code);
- }
- });
- });
init(key : SymKey) : Promise<void>;
使用對稱密鑰初始化Mac計(jì)算
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | 是 | 共享對稱密鑰 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Mac algName is: " + mac.algName);
- var KeyBlob;
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
- promiseConvertKey.then(symKey => {
- var promiseMacInit = mac.init(symKey);
- return promiseMacInit;
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
update(input : DataBlob, callback : AsyncCallback<void>) : void;
傳入消息進(jìn)行Mac更新計(jì)算
Hmac算法多次調(diào)用update更新的代碼示例詳見開發(fā)指導(dǎo)“使用消息認(rèn)證碼操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
input | 是 | 傳入的消息 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var KeyBlob;
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- }
- mac.init(symKey, (err1, ) => {
- if (err1) {
- console.error("[Callback] err: " + err1.code);
- }
- let blob;
- mac.update(blob, (err2, data) => {
- if (err2) {
- console.error("[Callback] err: " + err2.code);
- }
- });
- });
- });
update(input : DataBlob) : Promise<void>;
傳入消息進(jìn)行Mac更新計(jì)算
Hmac算法多次調(diào)用update更新的代碼示例詳見開發(fā)指導(dǎo)“使用消息認(rèn)證碼操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
input | 是 | 傳入的消息 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Mac algName is: " + mac.algName);
- var KeyBlob;
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
- promiseConvertKey.then(symKey => {
- var promiseMacInit = mac.init(symKey);
- return promiseMacInit;
- }).then(() => {
- let blob;
- var promiseMacUpdate = mac.update(blob);
- return promiseMacUpdate;
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
doFinal(callback : AsyncCallback<DataBlob>) : void;
返回Mac的計(jì)算結(jié)果
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<DataBlob> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var KeyBlob;
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- }
- mac.init(symKey, (err1, ) => {
- if (err1) {
- console.error("[Callback] err: " + err1.code);
- }
- let blob;
- mac.update(blob, (err2, ) => {
- if (err2) {
- console.error("[Callback] err: " + err2.code);
- }
- mac.doFinal((err3, macOutput) => {
- if (err3) {
- console.error("[Callback] err: " + err3.code);
- } else {
- console.error("[Promise]: HMAC result: " + macOutput);
- }
- });
- });
- });
- });
doFinal() : Promise<DataBlob>
返回Mac的計(jì)算結(jié)果
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Mac algName is: " + mac.algName);
- var KeyBlob;
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
- promiseConvertKey.then(symKey => {
- var promiseMacInit = mac.init(symKey);
- return promiseMacInit;
- }).then(() => {
- let blob;
- var promiseMacUpdate = mac.update(blob);
- return promiseMacUpdate;
- }).then(() => {
- var PromiseMacDoFinal = mac.doFinal();
- return PromiseMacDoFinal;
- }).then(macOutput => {
- console.error("[Promise]: HMAC result: " + macOutput.data);
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
getMacLength() : number
獲取Mac消息認(rèn)證碼的長度(字節(jié)數(shù))
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
number | 返回mac計(jì)算結(jié)果的字節(jié)長度 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var mac;
- try {
- mac = cryptoFramework.createMac("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Mac algName is: " + mac.algName);
- var KeyBlob;
- var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
- var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
- promiseConvertKey.then(symKey => {
- var promiseMacInit = mac.init(symKey);
- return promiseMacInit;
- }).then(() => {
- let blob;
- var promiseMacUpdate = mac.update(blob);
- return promiseMacUpdate;
- }).then(() => {
- var PromiseMacDoFinal = mac.doFinal();
- return PromiseMacDoFinal;
- }).then(macOutput => {
- console.error("[Promise]: HMAC result: " + macOutput.data);
- let macLen = mac.getMacLength();
- console.error("MAC len: " + macLen);
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
createMd(algName : string) : Md
生成Md實(shí)例,用于進(jìn)行消息摘要的計(jì)算與操作。
支持的規(guī)格詳見框架概述“MD消息摘要算法規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 指定摘要算法,支持算法請參考“MD算法支持范圍”一節(jié) |
返回值:
類型 | 說明 |
---|---|
Md | 返回由輸入算法指定生成的Md對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- // 參數(shù)選擇請參考上述算法支持范圍
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
- }
update(input : DataBlob, callback : AsyncCallback<void>) : void;
傳入消息進(jìn)行Md更新計(jì)算
Md算法多次調(diào)用update更新的代碼示例詳見開發(fā)指導(dǎo)“使用摘要操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
input | 是 | 傳入的消息 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Md algName is: " + md.algName);
- let blob;
- md.update(blob, (err,) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- }
- });
update(input : DataBlob) : Promise<void>;
傳入消息進(jìn)行Md更新計(jì)算
Md算法多次調(diào)用update更新的代碼示例詳見開發(fā)指導(dǎo)“使用摘要操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
input | DataBlob | 是 | 傳入的消息 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Md algName is: " + md.algName);
- let blob;
- var promiseMdUpdate = md.update(blob);
- promiseMdUpdate.then(() => {
- // do something
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
digest(callback : AsyncCallback<DataBlob>) : void
返回Md的計(jì)算結(jié)果
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<DataBlob> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Md algName is: " + md.algName);
- let blob;
- md.update(blob, (err,) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- }
- md.digest((err1, mdOutput) => {
- if (err1) {
- console.error("[Callback] err: " + err1.code);
- } else {
- console.error("[Callback]: MD result: " + mdOutput);
- }
- });
- });
digest() : Promise<DataBlob>
返回Md的計(jì)算結(jié)果
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Md algName is: " + md.algName);
- let blob;
- var promiseMdUpdate = md.update(blob);
- promiseMdUpdate.then(() => {
- var PromiseMdDigest = md.digest();
- return PromiseMdDigest;
- }).then(mdOutput => {
- console.error("[Promise]: MD result: " + mdOutput.data);
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
getMdLength() : number
獲取Md消息摘要長度(字節(jié)數(shù))
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
number | 返回md計(jì)算結(jié)果的字節(jié)長度 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var md;
- try {
- md = cryptoFramework.createMd("SHA256");
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- console.error("Md algName is: " + md.algName);
- let blob;
- var promiseMdUpdate = md.update(blob);
- promiseMdUpdate.then(() => {
- var PromiseMdDigest = md.digest();
- return PromiseMdDigest;
- }).then(mdOutput => {
- console.error("[Promise]: MD result: " + mdOutput.data);
- let mdLen = md.getMdLength();
- console.error("MD len: " + mdLen);
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
createRandom() : Random
生成Random實(shí)例,用于進(jìn)行隨機(jī)數(shù)的計(jì)算與設(shè)置種子。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
Random | 返回由輸入算法指定生成的Random對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- try {
- var rand = cryptoFramework.createRandom();
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
Random類,調(diào)用Random方法可以進(jìn)行隨機(jī)數(shù)計(jì)算。調(diào)用前,需要通過createRandom構(gòu)造Random實(shí)例。
generateRandom(len : number, callback: AsyncCallback<DataBlob>) : void;
生成指定長度的隨機(jī)數(shù)
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
len | number | 是 | 表示生成隨機(jī)數(shù)的長度 |
callback | AsyncCallback<DataBlob> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var rand;
- try {
- rand = cryptoFramework.createRandom();
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- rand.generateRandom(12, (err, randData) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- } else {
- console.error("[Callback]: generate random result: " + randData.data);
- }
- });
generateRandom(len : number) : Promise<DataBlob>;
生成指定長度的隨機(jī)數(shù)
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
len | number | 是 | 表示生成隨機(jī)數(shù)的長度 |
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var rand;
- try {
- rand = cryptoFramework.createRandom();
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- var promiseGenerateRand = rand.generateRandom(12);
- promiseGenerateRand.then(randData => {
- console.error("[Promise]: rand result: " + randData.data);
- }).catch(error => {
- console.error("[Promise]: error: " + error.message);
- });
setSeed(seed : DataBlob) : void;
設(shè)置指定的種子
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
seed | DataBlob | 是 | 設(shè)置的種子 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- var rand;
- try {
- rand = cryptoFramework.createRandom();
- } catch (error) {
- console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
- }
- rand.generateRandom(12, (err, randData) => {
- if (err) {
- console.error("[Callback] err: " + err.code);
- } else {
- console.error("[Callback]: generate random result: " + randData.data);
- try {
- rand.setSeed(randData);
- } catch (error) {
- console.log("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message);
- }
- }
- });
加解密參數(shù),在進(jìn)行對稱加解密時(shí)需要構(gòu)造其子類對象,并將子類對象傳入init()方法。
適用于需要iv等參數(shù)的對稱加解密模式(對于無iv等參數(shù)的模式如ECB模式,無需構(gòu)造,在init()中傳入null即可)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
algName | string | 是 | 是 | 指明對稱加解密參數(shù)的算法模式??蛇x值如下: - "IvParamsSpec": 適用于CBC|CTR|OFB|CFB模式 - "GcmParamsSpec": 適用于GCM模式 - "CcmParamsSpec": 適用于CCM模式 |
由于init()的params參數(shù)是ParamsSpec類型(父類),而實(shí)際需要傳入具體的子類對象(如IvParamsSpec),因此在構(gòu)造子類對象時(shí)應(yīng)設(shè)置其父類ParamsSpec的algName參數(shù),使算法庫在init()時(shí)知道傳入的是哪種子類對象。
加解密參數(shù)ParamsSpec的子類,用于在對稱加解密時(shí)作為init()方法的參數(shù)。
適用于CBC、CTR、OFB、CFB這些僅使用iv作為參數(shù)的加解密模式。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
iv | 是 | 是 | 指明加解密參數(shù)iv。常見取值如下: - AES的CBC|CTR|OFB|CFB模式:iv長度為16字節(jié) - 3DES的CBC|OFB|CFB模式:iv長度為8字節(jié) |
傳入init()方法前需要指定其algName屬性(來源于父類ParamsSpec)。
加解密參數(shù)ParamsSpec的子類,用于在對稱加解密時(shí)作為init()方法的參數(shù)。
適用于GCM模式。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
iv | 是 | 是 | 指明加解密參數(shù)iv,長度為12字節(jié) | |
aad | 是 | 是 | 指明加解密參數(shù)aad,長度為8字節(jié) | |
authTag | 是 | 是 | 指明加解密參數(shù)authTag,長度為16字節(jié)。 采用GCM模式加密時(shí),需要獲取doFinal()輸出的DataBlob,取出其末尾16字節(jié)作為解密時(shí)init()方法的入?yún)?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >GcmParamsSpec中的的authTag |
傳入init()方法前需要指定其algName屬性(來源于父類ParamsSpec)。
加解密參數(shù)ParamsSpec的子類,用于在對稱加解密時(shí)作為init()方法的參數(shù)。
適用于CCM模式。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
iv | 是 | 是 | 指明加解密參數(shù)iv,長度為7字節(jié) | |
aad | 是 | 是 | 指明加解密參數(shù)aad,長度為8字節(jié) | |
authTag | 是 | 是 | 指明加解密參數(shù)authTag,長度為12字節(jié)。 采用CCM模式加密時(shí),需要獲取doFinal()輸出的DataBlob,取出其末尾12字節(jié)作為解密時(shí)init()方法的入?yún)?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >CcmParamsSpec中的authTag |
傳入init()方法前需要指定其algName屬性(來源于父類ParamsSpec)。
表示加解密操作的枚舉。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 值 | 說明 |
---|---|---|
ENCRYPT_MODE | 0 | 表示進(jìn)行加密操作 |
DECRYPT_MODE | 1 | 表示進(jìn)行解密操作 |
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
format | string | 是 | 否 | 密鑰的格式。 |
algName | string | 是 | 否 | 密鑰對應(yīng)的算法名(含長度)。 |
getEncoded() : DataBlob
以同步方法,獲取16進(jìn)制形式的密鑰內(nèi)容。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
用于查看密鑰的具體內(nèi)容。 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function uint8ArrayToShowStr(uint8Array) {
- return Array.prototype.map
- .call(uint8Array, (x) => ('00' + x.toString(16)).slice(-2))
- .join('');
- }
- let key; // key為使用對稱密鑰生成器 生成的密鑰,此處省略生成過程
- let encodedKey = key.getEncoded();
- console.info("key hex:" + uint8ArrayToShowStr(encodedKey.data));
clearMem() : void
以同步方法,將系統(tǒng)底層內(nèi)存中的的密鑰內(nèi)容清零。建議在不再使用對稱密鑰實(shí)例時(shí),調(diào)用本函數(shù),避免內(nèi)存中密鑰數(shù)據(jù)存留過久。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function uint8ArrayToShowStr(uint8Array) {
- return Array.prototype.map
- .call(uint8Array, (x) => ('00' + x.toString(16)).slice(-2))
- .join('');
- }
- let key; // key為使用對稱密鑰生成器 生成的密鑰,此處省略生成過程
- let encodedKey = key.getEncoded();
- console.info("key hex:" + uint8ArrayToShowStr(encodedKey.data)); // 輸出密鑰內(nèi)容
- key.clearMem();
- encodedKey = key.getEncoded();
- console.info("key hex:" + uint8ArrayToShowStr(encodedKey.data)); // 輸出全零
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
format | string | 是 | 否 | 密鑰的格式。 |
algName | string | 是 | 否 | 密鑰對應(yīng)的算法名(含長度)。 |
getEncoded() : DataBlob
以同步方法,獲取二進(jìn)制形式的密鑰內(nèi)容。公鑰格式滿足ASN.1語法、X.509規(guī)范、DER編碼格式。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
用于查看密鑰的具體內(nèi)容。 |
示例:
- function uint8ArrayToShowStr(uint8Array) {
- return Array.prototype.map
- .call(uint8Array, (x) => ('00' + x.toString(16)).slice(-2))
- .join('');
- }
- let key; // key為使用非對稱密鑰生成器生成的非對稱密鑰的公鑰對象,此處省略生成過程
- console.info("key format:" + key.format);
- console.info("key algName:" + key.algName);
- var encodedKey = key.getEncoded();
- console.info("key encoded:" + uint8ArrayToShowStr(encodedKey.data));
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
format | string | 是 | 否 | 密鑰的格式。 |
algName | string | 是 | 否 | 密鑰對應(yīng)的算法名(含長度)。 |
getEncoded() : DataBlob
以同步方法,獲取二進(jìn)制形式的密鑰內(nèi)容。私鑰格式滿足ASN.1語法,PKCS#8規(guī)范、DER編碼方式。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
用于查看密鑰的具體內(nèi)容。 |
示例:
- function uint8ArrayToShowStr(uint8Array) {
- return Array.prototype.map
- .call(uint8Array, (x) => ('00' + x.toString(16)).slice(-2))
- .join('');
- }
- let key; // key為使用非對稱密鑰生成器生成的非對稱密鑰的私鑰對象,此處省略生成過程
- console.info("key format:" + key.format);
- console.info("key algName:" + key.algName);
- var encodedKey = key.getEncoded();
- console.info("key encoded:" + uint8ArrayToShowStr(encodedKey.data));
clearMem() : void
以同步方法,將系統(tǒng)底層內(nèi)存中的的密鑰內(nèi)容清零。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
示例:
- let key; // key為使用非對稱密鑰生成器生成的非對稱密鑰的私鑰對象,此處省略生成過程
- key.clearMem();
非對稱密鑰對,包含:公鑰與私鑰。
可以通過非對稱密鑰生成器AsyKeyGenerator來生成。
KeyPair對象中的pubKey對象和priKey對象,作為KeyPair對象中的一個(gè)參數(shù)存在,當(dāng)離開KeyPair對象作用域時(shí),其內(nèi)部對象可能被析構(gòu)。
業(yè)務(wù)方使用時(shí)應(yīng)持有KeyPair對象的引用,而非內(nèi)部pubKey或priKey對象的引用。
createSymKeyGenerator(algName : string) : SymKeyGenerator
通過指定算法名稱的字符串,獲取相應(yīng)的對稱密鑰生成器實(shí)例。
支持的規(guī)格詳見框架概述“密鑰生成規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 待生成對稱密鑰生成器的算法名稱。 具體取值詳見框架概述“密鑰生成規(guī)格”一節(jié)中的“字符串參數(shù)”。 |
返回值:
類型 | 說明 |
---|---|
返回對稱密鑰生成器的對象。 |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
generateSymKey(callback : AsyncCallback<SymKey>) : void
異步獲取對稱密鑰生成器隨機(jī)生成的密鑰,通過注冊回調(diào)函數(shù)獲取結(jié)果。
必須在使用createSymKeyGenerator創(chuàng)建對稱密鑰生成器后,才能使用本函數(shù)。
目前支持使用OpenSSL的RAND_priv_bytes()作為底層能力生成隨機(jī)密鑰。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<SymKey> | 是 | 回調(diào)函數(shù)。當(dāng)生成對稱密鑰成功,err為undefined,data為獲取到的SymKey;否則為錯(cuò)誤對象。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let symAlgName = '3DES192';
- let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
- symKeyGenerator.generateSymKey((err, symKey) => {
- if (err) {
- console.error(`Generate symKey failed, ${err.code}, ${err.message}`);
- } else {
- console.info(`Generate symKey success, algName: ${symKey.algName}`);
- }
- })
generateSymKey() : Promise<SymKey>
異步獲取該對稱密鑰生成器隨機(jī)生成的密鑰,通過Promise獲取結(jié)果。
必須在使用createSymKeyGenerator創(chuàng)建對稱密鑰生成器后,才能使用本函數(shù)。
目前支持使用OpenSSL的RAND_priv_bytes()作為底層能力生成隨機(jī)密鑰。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
Promise<SymKey> | Promise對象,返回對稱密鑰SymKey。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let symAlgName = 'AES128';
- let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
- symKeyGenerator.generateSymKey()
- .then(symKey => {
- console.info(`Generate symKey success, algName: ${symKey.algName}`);
- }, error => {
- console.error(`Generate symKey failed, ${error.code}, ${error.message}`);
- })
convertKey(key : DataBlob, callback : AsyncCallback<SymKey>) : void
異步根據(jù)指定數(shù)據(jù)生成對稱密鑰,通過注冊回調(diào)函數(shù)獲取結(jié)果。
必須在使用createSymKeyGenerator創(chuàng)建對稱密鑰生成器后,才能使用本函數(shù)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- function genKeyMaterialBlob() {
- let arr = [
- 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
- 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
- 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes)
- let keyMaterial = new Uint8Array(arr);
- return {data : keyMaterial};
- }
- let symAlgName = '3DES192';
- let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
- let keyMaterialBlob = genKeyMaterialBlob();
- symKeyGenerator.convertKey(keyMaterialBlob, (err, symKey) => {
- if (err) {
- console.error(`Convert symKey failed, ${err.code}, ${err.message}`);
- } else {
- console.info(`Convert symKey success, algName: ${symKey.algName}`);
- }
- })
convertKey(key : DataBlob) : Promise<SymKey>
異步根據(jù)指定數(shù)據(jù)生成對稱密鑰,通過Promise獲取結(jié)果。
必須在使用createSymKeyGenerator創(chuàng)建對稱密鑰生成器后,才能使用本函數(shù)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
key | 是 | 指定的密鑰材料數(shù)據(jù)。 |
返回值:
類型 | 說明 |
---|---|
Promise<SymKey> | Promise對象,返回對稱密鑰SymKey。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- function genKeyMaterialBlob() {
- let arr = [
- 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
- 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
- 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes)
- let keyMaterial = new Uint8Array(arr);
- return {data : keyMaterial};
- }
- let symAlgName = '3DES192';
- let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName);
- let keyMaterialBlob = genKeyMaterialBlob();
- symKeyGenerator.convertKey(keyMaterialBlob)
- .then(symKey => {
- console.info(`Convert symKey success, algName: ${symKey.algName}`);
- }, error => {
- console.error(`Convert symKey failed, ${error.code}, ${error.message}`);
- })
createAsyKeyGenerator(algName : string) : AsyKeyGenerator
通過指定算法名稱的字符串,獲取相應(yīng)的非對稱密鑰生成器實(shí)例。
支持的規(guī)格詳見框架概述“密鑰生成規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 待生成對稱密鑰生成器的算法名稱。 |
返回值:
類型 | 說明 |
---|---|
返回非對稱密鑰生成器的對象。 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
非對稱密鑰生成器。在使用該類的方法前,需要先使用createAsyKeyGenerator()方法構(gòu)建一個(gè)AsyKeyGenerator實(shí)例。
generateKeyPair(callback : AsyncCallback<KeyPair>) : void;
異步獲取非對稱密鑰生成器隨機(jī)生成的密鑰,通過注冊回調(diào)函數(shù)獲取結(jié)果。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<KeyPair> | 是 | 回調(diào)函數(shù),用于獲取非對稱密鑰。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
- asyKeyGenerator.generateKeyPair((err, keyPair) => {
- if (err) {
- console.error("generateKeyPair: error.");
- return;
- }
- console.info("generateKeyPair: success.");
- })
generateKeyPair() : Promise<KeyPair>
異步獲取該非對稱密鑰生成器隨機(jī)生成的密鑰,通過Promise獲取結(jié)果。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
返回值:
類型 | 說明 |
---|---|
Promise<KeyPair> | 使用Promise的方式獲取非對稱密鑰。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
- let keyGenPromise = asyKeyGenerator.generateKeyPair();
- keyGenPromise.then( keyPair => {
- console.info("generateKeyPair success.");
- }).catch(error => {
- console.error("generateKeyPair error.");
- });
convertKey(pubKey : DataBlob, priKey : DataBlob, callback : AsyncCallback<KeyPair>) : void
異步獲取指定數(shù)據(jù)生成非對稱密鑰,通過注冊回調(diào)函數(shù)獲取結(jié)果。詳情請看下方密鑰轉(zhuǎn)換說明
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let pubKey; // X.509規(guī)范、DER格式的公鑰數(shù)據(jù),此處省略數(shù)據(jù)。
- let priKey; // PKCS#8規(guī)范、DER格式的私鑰數(shù)據(jù),此處省略數(shù)據(jù)。
- let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
- asyKeyGenerator.convertKey(pubKey, priKey, (err, keyPair) => {
- if (err) {
- console.error("convertKey: error.");
- return;
- }
- console.info("convertKey: success.");
- })
convertKey(pubKey : DataBlob, priKey : DataBlob) : Promise<KeyPair>
異步獲取指定數(shù)據(jù)生成非對稱密鑰,通過Promise獲取結(jié)果。詳情請看下方密鑰轉(zhuǎn)換說明
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
pubKey | DataBlob | 是 | 指定的公鑰材料。如果公鑰不需要轉(zhuǎn)換,可直接傳入null |
priKey | DataBlob | 是 | 指定的私鑰材料。如果私鑰不需要轉(zhuǎn)換,可直接傳入null |
返回值:
類型 | 說明 |
---|---|
Promise<KeyPair> | 使用Promise的方式獲取非對稱密鑰。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
- let pubKey; // pubKey為使用非對稱密鑰生成器生成的非對稱密鑰的公鑰對象,此處省略生成過程
- let priKey; // priKey為使用非對稱密鑰生成器生成的非對稱密鑰的私鑰對象,此處省略生成過程
- let keyGenPromise = asyKeyGenerator.convertKey(pubKey, priKey);
- keyGenPromise.then( keyPair => {
- console.info("convertKey success.");
- }).catch(error => {
- console.error("convertKey error.");
- });
密鑰轉(zhuǎn)換說明
createCipher(transformation : string) : Cipher
通過指定算法名稱,獲取相應(yīng)的Cipher實(shí)例。
支持的規(guī)格詳見框架概述“加解密規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
transformation | string | 是 | 待生成Cipher的算法名稱(含密鑰長度)、加密模式以及填充方法的組合。 具體取值詳見框架概述“加解密規(guī)格”一節(jié)中的“字符串參數(shù)”。 |
目前對稱加解密中,PKCS5和PKCS7的實(shí)現(xiàn)相同,其padding長度和分組長度保持一致(即PKCS5和PKCS7在3DES中均按照8字節(jié)填充,在AES中均按照16字節(jié)填充),另有NoPadding表示不填充。
開發(fā)者需要自行了解密碼學(xué)不同分組模式的差異,以便選擇合適的參數(shù)規(guī)格。例如選擇ECB和CBC模式時(shí),建議啟用填充,否則必須確保明文長度是分組大小的整數(shù)倍;選擇其他模式時(shí),可以不啟用填充,此時(shí)密文長度和明文長度一致(即可能不是分組大小的整數(shù)倍)。
返回值:
類型 | 說明 |
---|---|
返回加解密生成器的對象。 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let cipherAlgName = '3DES192|ECB|PKCS7';
- var cipher;
- try {
- cipher = cryptoFramework.createCipher(cipherAlgName);
- console.info(`cipher algName: ${cipher.algName}`);
- } catch (error) {
- console.error(`createCipher failed, ${error.code}, ${error.message}`);
- }
提供加解密的算法操作功能,按序調(diào)用本類中的init()、update()、doFinal()方法,可以實(shí)現(xiàn)對稱加密/對稱解密/非對稱加密/非對稱解密。
完整的加解密流程示例可參考開發(fā)指導(dǎo)中的“使用加解密操作”一節(jié)。
一次完整的加/解密流程在對稱加密和非對稱加密中略有不同:
init(opMode : CryptoMode, key : Key, params : ParamsSpec, callback : AsyncCallback<void>) : void
初始化加解密的cipher對象,通過注冊回調(diào)函數(shù)獲取結(jié)果。
必須在使用createCipher創(chuàng)建Cipher實(shí)例后,才能使用本函數(shù)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
opMode | 是 | 加密或者解密模式。 | |
key | 是 | 指定加密或解密的密鑰。 | |
params | 是 | 指定加密或解密的參數(shù),對于ECB等沒有參數(shù)的算法模式,可以傳入null。 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當(dāng)初始化成功,err為undefined,否則為錯(cuò)誤對象。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let symKey; // 此處省略生成對稱密鑰的過程
- let cipher; // 此處省略生成cipher實(shí)例的過程
- cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null, (err, ) => {
- if (err) {
- console.error(`Failed to init cipher, ${err.code}, ${err.message}`);
- } else {
- console.info(`Init cipher success`);
- // 此處進(jìn)行update等后續(xù)操作
- }
- })
init(opMode : CryptoMode, key : Key, params : ParamsSpec) : Promise<void>
初始化加解密的cipher對象,通過Promise獲取結(jié)果。
必須在使用createCipher創(chuàng)建Cipher實(shí)例后,才能使用本函數(shù)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
opMode | 是 | 加密或者解密模式。 | |
key | 是 | 指定加密或解密的密鑰。 | |
params | 是 | 指定加密或解密的參數(shù),對于ECB等沒有參數(shù)的算法模式,可以傳入null。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象。無返回結(jié)果的Promise對象。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let symKey; // 此處省略生成對稱密鑰的過程
- let cipher; // 此處省略生成cipher實(shí)例的過程
- cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null)
- .then(() => {
- console.info(`Init cipher success`);
- // 此處進(jìn)行update等后續(xù)操作
- }, error => {
- console.error(`Failed to init cipher, ${error.code}, ${error.message}`);
- })
update(data : DataBlob, callback : AsyncCallback<DataBlob>) : void
分段更新加密或者解密數(shù)據(jù)操作,通過注冊回調(diào)函數(shù)獲取加/解密數(shù)據(jù)。
必須在對Cipher實(shí)例使用init()初始化后,才能使用本函數(shù)。
在進(jìn)行對稱加解密操作的時(shí)候,如果開發(fā)者對各個(gè)分組模式不夠熟悉,建議對每次update和doFinal的結(jié)果都判斷是否為null,并在結(jié)果不為null時(shí)取出其中的數(shù)據(jù)進(jìn)行拼接,形成完整的密文/明文。這是因?yàn)檫x擇的分組模式等各項(xiàng)規(guī)格都可能對update和doFinal結(jié)果產(chǎn)生影響。
(例如對于ECB和CBC模式,不論update傳入的數(shù)據(jù)是否為分組長度的整數(shù)倍,都會以分組作為基本單位進(jìn)行加/解密,并輸出本次update新產(chǎn)生的加/解密分組結(jié)果。
可以理解為,update只要湊滿一個(gè)新的分組就會有輸出,如果沒有湊滿則此次update輸出為null,把當(dāng)前還沒被加/解密的數(shù)據(jù)留著,等下一次update/doFinal傳入數(shù)據(jù)的時(shí)候,拼接起來繼續(xù)湊分組。
最后doFinal的時(shí)候,會把剩下的還沒加/解密的數(shù)據(jù),根據(jù)createCipher時(shí)設(shè)置的padding模式進(jìn)行填充,補(bǔ)齊到分組的整數(shù)倍長度,再輸出剩余加解密結(jié)果。
而對于可以將分組密碼轉(zhuǎn)化為流模式實(shí)現(xiàn)的模式,還可能出現(xiàn)密文長度和明文長度相同的情況等。)
根據(jù)數(shù)據(jù)量,可以不調(diào)用update(即init完成后直接調(diào)用doFinal)或多次調(diào)用update。
算法庫目前沒有對update(單次或累計(jì))的數(shù)據(jù)量設(shè)置大小限制,建議對于大數(shù)據(jù)量的對稱加解密,采用多次update的方式傳入數(shù)據(jù)。
AES使用多次update操作的示例代碼詳見開發(fā)指導(dǎo)“使用加解密操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- function stringToUint8Array(str) {
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- return new Uint8Array(arr);
- }
- let cipher; // 此處省略生成cipher實(shí)例的過程
- // 此處省略init()過程
- let plainText = {data : stringToUint8Array('this is test!')};
- cipher.update(plainText, (err, output) => { // 加密過程舉例
- if (err) {
- console.error(`Failed to update cipher`);
- } else {
- console.info(`Update cipher success`);
- if (output != null) {
- // 拼接output.data到密文
- }
- // 此處進(jìn)行doFinal等后續(xù)操作
- }
- })
update(data : DataBlob) : Promise<DataBlob>
分段更新加密或者解密數(shù)據(jù)操作,通過Promise獲取加/解密數(shù)據(jù)。
必須在對Cipher實(shí)例使用init()初始化后,才能使用本函數(shù)。
在進(jìn)行對稱加解密操作的時(shí)候,如果開發(fā)者對各個(gè)分組模式不夠熟悉,建議對每次update和doFinal的結(jié)果都判斷是否為null,并在結(jié)果不為null時(shí)取出其中的數(shù)據(jù)進(jìn)行拼接,形成完整的密文/明文。這是因?yàn)檫x擇的分組模式等各項(xiàng)規(guī)格都可能對update和doFinal結(jié)果產(chǎn)生影響。
(例如對于ECB和CBC模式,不論update傳入的數(shù)據(jù)是否為分組長度的整數(shù)倍,都會以分組作為基本單位進(jìn)行加/解密,并輸出本次update新產(chǎn)生的加/解密分組結(jié)果。
可以理解為,update只要湊滿一個(gè)新的分組就會有輸出,如果沒有湊滿則此次update輸出為null,把當(dāng)前還沒被加/解密的數(shù)據(jù)留著,等下一次update/doFinal傳入數(shù)據(jù)的時(shí)候,拼接起來繼續(xù)湊分組。
最后doFinal的時(shí)候,會把剩下的還沒加/解密的數(shù)據(jù),根據(jù)createCipher時(shí)設(shè)置的padding模式進(jìn)行填充,補(bǔ)齊到分組的整數(shù)倍長度,再輸出剩余加解密結(jié)果。
而對于可以將分組密碼轉(zhuǎn)化為流模式實(shí)現(xiàn)的模式,還可能出現(xiàn)密文長度和明文長度相同的情況等。)
根據(jù)數(shù)據(jù)量,可以不調(diào)用update(即init完成后直接調(diào)用doFinal)或多次調(diào)用update。
算法庫目前沒有對update(單次或累計(jì))的數(shù)據(jù)量設(shè)置大小限制,建議對于大數(shù)據(jù)量的對稱加解密,可以采用多次update的方式傳入數(shù)據(jù)。
AES使用多次update操作的示例代碼詳見開發(fā)指導(dǎo)“使用加解密操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 加密或者解密的數(shù)據(jù)。data不能為null,也不允許傳入{data : Uint8Array(空) } |
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | Promise對象,返回此次更新的加/解密結(jié)果DataBlob。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- function stringToUint8Array(str) {
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- return new Uint8Array(arr);
- }
- let cipher; // 此處省略生成cipher實(shí)例的過程
- // 此處省略init()過程
- let plainText = {data : stringToUint8Array('this is test!')};
- cipher.update(plainText)
- .then((output) => {
- console.info(`Update cipher success.`);
- if (output != null) {
- // 拼接output.data到密文
- }
- // 此處進(jìn)行doFinal等后續(xù)操作
- }, error => {
- console.info(`Update cipher failed.`);
- })
doFinal(data : DataBlob, callback : AsyncCallback<DataBlob>) : void
(1)在對稱加解密中,doFinal加/解密(分組模式產(chǎn)生的)剩余數(shù)據(jù)和本次傳入的數(shù)據(jù),最后結(jié)束加密或者解密數(shù)據(jù)操作,通過注冊回調(diào)函數(shù)獲取加密或者解密數(shù)據(jù)。
如果數(shù)據(jù)量較小,可以在doFinal中一次性傳入數(shù)據(jù),而不使用update;如果在本次加解密流程中,已經(jīng)使用update傳入過數(shù)據(jù),可以在doFinal的data參數(shù)處傳入null。
根據(jù)對稱加解密的模式不同,doFinal的輸出有如下區(qū)別:
對于GCM和CCM模式的對稱加密:一次加密流程中,如果將每一次update和doFinal的結(jié)果拼接起來,會得到“密文+authTag”,即末尾的16字節(jié)(GCM模式)或12字節(jié)(CCM模式)是authTag,而其余部分均為密文。(也就是說,如果doFinal的data參數(shù)傳入null,則doFinal的結(jié)果就是authTag)
authTag需要填入解密時(shí)的GcmParamsSpec或CcmParamsSpec;密文則作為解密時(shí)的入?yún)ata。
(2)在RSA非對稱加解密中,doFinal加/解密本次傳入的數(shù)據(jù),通過注冊回調(diào)函數(shù)獲取加密或者解密數(shù)據(jù)。如果數(shù)據(jù)量較大,可以多次調(diào)用doFinal,拼接結(jié)果得到完整的明文/密文。
對稱加解密中,調(diào)用doFinal標(biāo)志著一次加解密流程已經(jīng)完成,即Cipher實(shí)例的狀態(tài)被清除,因此當(dāng)后續(xù)開啟新一輪加解密流程時(shí),需要重新調(diào)用init()并傳入完整的參數(shù)列表進(jìn)行初始化
(比如即使是對同一個(gè)Cipher實(shí)例,采用同樣的對稱密鑰,進(jìn)行加密然后解密,則解密中調(diào)用init的時(shí)候仍需填寫params參數(shù),而不能直接省略為null)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let cipher; // 此處省略生成cipher實(shí)例的過程
- let data; // 此處省略準(zhǔn)備待加密/解密數(shù)據(jù)的過程
- // 此處省略init()和update()過程
- cipher.doFinal(data, (err, output) => {
- if (err) {
- console.error(`Failed to finalize cipher, ${err.code}, ${err.message}`);
- } else {
- console.info(`Finalize cipher success`);
- if (output != null) {
- // 拼接output.data得到完整的明文/密文(及authTag)
- }
- }
- })
doFinal(data : DataBlob) : Promise<DataBlob>
(1)在對稱加解密中,doFinal加/解密(分組模式產(chǎn)生的)剩余數(shù)據(jù)和本次傳入的數(shù)據(jù),最后結(jié)束加密或者解密數(shù)據(jù)操作,通過Promise獲取加密或者解密數(shù)據(jù)。
如果數(shù)據(jù)量較小,可以在doFinal中一次性傳入數(shù)據(jù),而不使用update;如果在本次加解密流程中,已經(jīng)使用update傳入過數(shù)據(jù),可以在doFinal的data參數(shù)處傳入null。
根據(jù)對稱加解密的模式不同,doFinal的輸出有如下區(qū)別:
對于GCM和CCM模式的對稱加密:一次加密流程中,如果將每一次update和doFinal的結(jié)果拼接起來,會得到“密文+authTag”,即末尾的16字節(jié)(GCM模式)或12字節(jié)(CCM模式)是authTag,而其余部分均為密文。(也就是說,如果doFinal的data參數(shù)傳入null,則doFinal的結(jié)果就是authTag)
authTag需要填入解密時(shí)的GcmParamsSpec或CcmParamsSpec;密文則作為解密時(shí)的入?yún)ata。
(2)在RSA非對稱加解密中,doFinal加/解密本次傳入的數(shù)據(jù),通過Promise獲取加密或者解密數(shù)據(jù)。如果數(shù)據(jù)量較大,可以多次調(diào)用doFinal,拼接結(jié)果得到完整的明文/密文。
對稱加解密中,調(diào)用doFinal標(biāo)志著一次加解密流程已經(jīng)完成,即Cipher實(shí)例的狀態(tài)被清除,因此當(dāng)后續(xù)開啟新一輪加解密流程時(shí),需要重新調(diào)用init()并傳入完整的參數(shù)列表進(jìn)行初始化
(比如即使是對同一個(gè)Cipher實(shí)例,采用同樣的對稱密鑰,進(jìn)行加密然后解密,則解密中調(diào)用init的時(shí)候仍需填寫params參數(shù),而不能直接省略為null)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 加密或者解密的數(shù)據(jù)。data參數(shù)允許為null,但不允許傳入{data : Uint8Array(空) } |
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | Promise對象,返回剩余數(shù)據(jù)的加/解密結(jié)果DataBlob。 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
示例:
- import cryptoFramework from '@ohos.security.cryptoFramework';
- let cipher; // 此處省略生成cipher實(shí)例的過程
- let data; // 此處省略準(zhǔn)備待加密/解密數(shù)據(jù)的過程
- // 此處省略init()和update()過程
- cipher.doFinal(data)
- .then(output => {
- console.info(`Finalize cipher success`);
- if (output != null) {
- // 拼接output.data得到完整的明文/密文(及authTag)
- }
- }, error => {
- console.error(`Failed to finalize cipher, ${error.code}, ${error.message}`);
- })
使用RSA加密的callback完整示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function stringToUint8Array(str) {
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- return new Uint8Array(arr);
- }
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
- let cipher = cryptoFramework.createCipher("RSA1024|PKCS1");
- rsaGenerator.generateKeyPair(function (err, keyPair) {
- let pubKey = keyPair.pubKey;
- cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null, function (err, data) {
- let plainText = "this is cipher text";
- let input = {data : stringToUint8Array(plainText) };
- cipher.doFinal(input, function (err, data) {
- AlertDialog.show({ message : "EncryptOutPut is " + data.data} );
- });
- });
- });
使用RSA加密的promise完整示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function stringToUint8Array(str) {
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- return new Uint8Array(arr);
- }
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
- let cipher = cryptoFramework.createCipher("RSA1024|PKCS1");
- let keyGenPromise = rsaGenerator.generateKeyPair();
- keyGenPromise.then(rsaKeyPair => {
- let pubKey = rsaKeyPair.pubKey;
- return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); // 傳入私鑰和DECRYPT_MODE可初始化解密模式
- }).then(() => {
- let plainText = "this is cipher text";
- let input = { data : stringToUint8Array(plainText) };
- return cipher.doFinal(input);
- }).then(dataBlob => {
- console.info("EncryptOutPut is " + dataBlob.data);
- });
更多加解密流程的完整示例可參考開發(fā)指導(dǎo)中的“使用加解密操作”一節(jié)。
createSign(algName : string) : Sign
Sign實(shí)例生成。
支持的規(guī)格詳見框架概述“簽名驗(yàn)簽規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 指定簽名算法:RSA或ECC,使用RSA PKCS1模式時(shí)需要設(shè)置摘要,使用RSA PSS模式時(shí)需要設(shè)置摘要和掩碼摘要 |
返回值:
類型 | 說明 |
---|---|
Sign | 返回由輸入算法指定生成的Sign對象 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let signer1 = cryptoFramework.createSign("RSA1024|PKCS1|SHA256");
- let singer2 = cryptoFramework.createSign("RSA1024|PSS|SHA256|MGF1_SHA256")
Sign類,使用Sign方法之前需要?jiǎng)?chuàng)建該類的實(shí)例進(jìn)行操作,通過createSign(algName : string) : Sign方法構(gòu)造此實(shí)例。Sign類不支持重復(fù)初始化,當(dāng)業(yè)務(wù)方需要使用新密鑰簽名時(shí),需要重新創(chuàng)建新Sign對象并調(diào)用init初始化。
業(yè)務(wù)方使用時(shí),在createSign時(shí)確定簽名的模式,調(diào)用init接口設(shè)置密鑰。
當(dāng)待簽名數(shù)據(jù)較短時(shí),可在init初始化后直接調(diào)用sign接口傳入原文數(shù)據(jù)進(jìn)行簽名。
當(dāng)待簽名數(shù)據(jù)較長時(shí),可通過update接口分段傳入切分后的原文數(shù)據(jù),最后調(diào)用sign接口對整體原文數(shù)據(jù)進(jìn)行簽名。
當(dāng)使用update分段傳入原文時(shí),sign接口支持傳null,業(yè)務(wù)方可在循環(huán)中調(diào)用update接口,循環(huán)結(jié)束后調(diào)用sign進(jìn)行簽名。
init(priKey : PriKey, callback : AsyncCallback<void>) : void
使用私鑰初始化Sign對象,Callback形式,Sign類暫不支持重復(fù)init
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
priKey | 是 | 用于Sign的初始化 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
init(priKey : PriKey) : Promise<void>
使用私鑰初始化Sign對象,Promise形式,Sign類暫不支持重復(fù)init
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
priKey | 是 | 用于Sign的初始化 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
update(data : DataBlob, callback : AsyncCallback<void>) : void
追加待簽名數(shù)據(jù),callback方式
Sign多次調(diào)用update的代碼示例詳見開發(fā)指導(dǎo)“使用簽名驗(yàn)簽操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 傳入的消息 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
update(data : DataBlob) : Promise<void>;
追加待簽名數(shù)據(jù),promise方式
Sign多次調(diào)用update的代碼示例詳見開發(fā)指導(dǎo)“使用簽名驗(yàn)簽操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 傳入的消息 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
sign(data : DataBlob, callback : AsyncCallback<DataBlob>) : void
對數(shù)據(jù)進(jìn)行簽名,返回簽名結(jié)果,callback方式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
sign(data : DataBlob) : Promise<DataBlob>
對數(shù)據(jù)進(jìn)行簽名,返回簽名結(jié)果,promise方式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 傳入的消息 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
callback示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function stringToUint8Array(str) {
- var arr = [];
- for (var i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- var tmpArray = new Uint8Array(arr);
- return tmpArray;
- }
- let globalKeyPair;
- let SignMessageBlob;
- let plan1 = "This is Sign test plan1"; // The first segment of data.
- let plan2 = "This is Sign test plan2"; // The second segment of fata.
- let input1 = { data : stringToUint8Array(plan1) };
- let input2 = { data : stringToUint8Array(plan2) };
- function signMessageCallback() {
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
- let signer = cryptoFramework.createSign("RSA1024|PKCS1|SHA256");
- rsaGenerator.generateKeyPair(function (err, keyPair) {
- globalKeyPair = keyPair;
- let priKey = globalKeyPair.priKey;
- signer.init(priKey, function (err, data) {
- signer.update(input1, function (err, data) { // add first segment of data
- signer.sign(input2, function (err, data) { // add second segment of data, sign input1 and input2
- SignMessageBlob = data;
- AlertDialog.show({message : "res" + SignMessageBlob.data});
- });
- });
- });
- });
- }
promise示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- function stringToUint8Array(str) {
- var arr = [];
- for (var i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- var tmpArray = new Uint8Array(arr);
- return tmpArray;
- }
- let globalKeyPair;
- let SignMessageBlob;
- let plan1 = "This is Sign test plan1"; // The first segment of data.
- let plan2 = "This is Sign test plan2"; // The second segment of fata.
- let input1 = { data : stringToUint8Array(plan1) };
- let input2 = { data : stringToUint8Array(plan2) };
- function signMessagePromise() {
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
- let signer = cryptoFramework.createSign("RSA1024|PKCS1|SHA256");
- let keyGenPromise = rsaGenerator.generateKeyPair();
- keyGenPromise.then( keyPair => {
- globalKeyPair = keyPair;
- let priKey = globalKeyPair.priKey;
- return signer.init(priKey);
- }).then(() => {
- return signer.update(input1); // add first segment of data
- }).then(() => {
- return signer.sign(input2); // add second segment of data, sign input1 and input2
- }).then(dataBlob => {
- SignMessageBlob = dataBlob;
- console.info("sign output is " + SignMessageBlob.data);
- AlertDialog.show({message : "output" + SignMessageBlob.data});
- });
- }
createVerify(algName : string) : Verify
Verify實(shí)例生成。
支持的規(guī)格詳見框架概述“簽名驗(yàn)簽規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 指定簽名算法:RSA或ECC,使用RSA PKCS1模式時(shí)需要設(shè)置摘要,使用RSA PSS模式時(shí)需要設(shè)置摘要和掩碼摘要 |
返回值:
類型 | 說明 |
---|---|
Verify | 返回由輸入算法指定生成的Verify對象 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let verifyer1 = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256");
- let verifyer2 = cryptoFramework.createVerify("RSA1024|PSS|SHA256|MGF1_SHA256")
Verify類,使用Verify方法之前需要?jiǎng)?chuàng)建該類的實(shí)例進(jìn)行操作,通過createVerify(algName : string) : Verify方法構(gòu)造此實(shí)例。
Verify類不支持重復(fù)初始化,當(dāng)業(yè)務(wù)方需要使用新密鑰驗(yàn)簽時(shí),需要重新創(chuàng)建新Verify對象并調(diào)用init初始化。
業(yè)務(wù)方使用時(shí),在createVerify時(shí)確定驗(yàn)簽的模式,調(diào)用init接口設(shè)置密鑰。
當(dāng)簽名數(shù)據(jù)較短時(shí),可在init初始化后直接調(diào)用verify接口傳入簽名數(shù)據(jù)和原文進(jìn)行驗(yàn)簽。
當(dāng)簽名數(shù)據(jù)較長時(shí),可通過update接口分段傳入簽名數(shù)據(jù),最后調(diào)用verify接口對整體簽名數(shù)據(jù)進(jìn)行驗(yàn)簽。
當(dāng)使用update分段傳入簽名數(shù)據(jù)時(shí),verify接口的簽名數(shù)據(jù)支持傳null,業(yè)務(wù)方可在循環(huán)中調(diào)用update接口,循環(huán)結(jié)束后調(diào)用verify傳入原文進(jìn)行驗(yàn)簽。
init(pubKey : PubKey, callback : AsyncCallback<void>) : void
傳入公鑰初始化Verify對象,Callback形式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
pubKey | 是 | 公鑰對象,用于Verify的初始化 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
init(pubKey : PubKey) : Promise<void>
傳入公鑰初始化Verify對象,Promise形式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
pubKey | 是 | 公鑰對象,用于Verify的初始化 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
update(data : DataBlob, callback : AsyncCallback<void>) : void
追加待驗(yàn)簽數(shù)據(jù),callback方式
Verify多次調(diào)用update的代碼示例詳見開發(fā)指導(dǎo)“使用簽名驗(yàn)簽操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 傳入的消息 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù) |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
update(data : DataBlob) : Promise<void>;
追加待驗(yàn)簽數(shù)據(jù),promise方式
Verify多次調(diào)用update的代碼示例詳見開發(fā)指導(dǎo)“使用簽名驗(yàn)簽操作”。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | 傳入的消息 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise對象 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
verify(data : DataBlob, signatureData : DataBlob, callback : AsyncCallback<boolean>) : void
對數(shù)據(jù)進(jìn)行驗(yàn)簽,返回驗(yàn)簽結(jié)果,callback方式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
verify(data : DataBlob, signatureData : DataBlob) : Promise<boolean>
對數(shù)據(jù)進(jìn)行驗(yàn)簽,返回驗(yàn)簽結(jié)果,promise方式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
返回值:
類型 | 說明 |
---|---|
Promise<boolean> | 異步返回值,代表驗(yàn)簽是否通過 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
callback示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let globalKeyPair; // globalKeyPair為使用非對稱密鑰生成器生成的非對稱密鑰對象,此處省略生成過程
- let input1 = null;
- let input2 = null;
- let signMessageBlob = null; // 簽名后的數(shù)據(jù),此處省略
- let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA25");
- verifyer.init(globalKeyPair.pubKey, function (err, data) {
- verifyer.update(input1, function(err, data) {
- verifyer.verify(input2, signMessageBlob, function(err, data) {
- console.info("verify result is " + data);
- })
- });
- })
promise示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let globalKeyPair; // globalKeyPair為使用非對稱密鑰生成器生成的非對稱密鑰對象,此處省略生成過程
- let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256");
- let verifyInitPromise = verifyer.init(globalKeyPair.pubKey);
- let input1 = null;
- let input2 = null;
- let signMessageBlob = null; // 簽名后的數(shù)據(jù),此處省略
- verifyInitPromise.then(() => {
- return verifyer.update(input1);
- }).then(() => {
- return verifyer.verify(input2, signMessageBlob);
- }).then(res => {
- console.log("Verify result is " + res);
- });
createKeyAgreement(algName : string) : KeyAgreement
KeyAgreement實(shí)例生成。
支持的規(guī)格詳見框架概述“密鑰協(xié)商規(guī)格”一節(jié)。
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
algName | string | 是 | 指定密鑰協(xié)商算法:目前僅支持ECC |
返回值:
類型 | 說明 |
---|---|
KeyAgreement | 返回由輸入算法指定生成的KeyAgreement對象 |
示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let keyAgreement = cryptoFramework.createKeyAgreement("ECC256");
KeyAgreement類,使用密鑰協(xié)商方法之前需要?jiǎng)?chuàng)建該類的實(shí)例進(jìn)行操作,通過createKeyAgreement(algName : string) : KeyAgreement方法構(gòu)造此實(shí)例。
generateSecret(priKey : PriKey, pubKey : PubKey, callback : AsyncCallback<DataBlob>) : void
基于傳入的私鑰與公鑰進(jìn)行密鑰協(xié)商,返回共享秘密,Callback形式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
generateSecret(priKey : PriKey, pubKey : PubKey) : Promise<DataBlob>
基于傳入的私鑰與公鑰進(jìn)行密鑰協(xié)商,返回共享秘密,Promise形式
系統(tǒng)能力: SystemCapability.Security.CryptoFramework
參數(shù):
返回值:
類型 | 說明 |
---|---|
Promise<DataBlob> | 共享秘密 |
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
17620001 | memory error. |
17620002 | runtime error. |
17630001 | crypto operation error. |
callback示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let globalKeyPair; // globalKeyPair為使用非對稱密鑰生成器生成的非對稱密鑰對象,此處省略生成過程
- let keyAgreement = cryptoFramework.createKeyAgreement("ECC256");
- keyAgreement.generateSecret(globalKeyPair.priKey, globalKeyPair.pubKey, function (err, secret) {
- if (err) {
- console.error("keyAgreement error.");
- return;
- }
- console.info("keyAgreement output is " + secret.data);
- });
promise示例:
- import cryptoFramework from "@ohos.security.cryptoFramework"
- let globalKeyPair; // globalKeyPair為使用非對稱密鑰生成器生成的非對稱密鑰對象,此處省略生成過程
- let keyAgreement = cryptoFramework.createKeyAgreement("ECC256");
- let keyAgreementPromise = keyAgreement.generateSecret(globalKeyPair.priKey, globalKeyPair.pubKey);
- keyAgreementPromise.then((secret) => {
- console.info("keyAgreement output is " + secret.data);
- }).catch((error) => {
- console.error("keyAgreement error.");
更多建議: