From fbc9c37559d064648cb1dafbd9a5327348ff4bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 24 Mar 2025 11:36:49 +0800 Subject: [PATCH 1/7] get sec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../ani/bind_function_class.cpp | 2 ++ .../ani/ets/@ohos.file.securityLabel.ets | 29 +++++++++++++++++++ .../ani/securitylabel_ani.cpp | 28 ++++++++++++++++++ .../mod_securitylabel/ani/securitylabel_ani.h | 1 + .../mod_securitylabel/securitylabel_core.cpp | 6 ++++ .../mod_securitylabel/securitylabel_core.h | 1 + 6 files changed, 67 insertions(+) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 9dfe0fb3f..47b5daed2 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -25,6 +25,8 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, + ani_native_function { "getSecurityLabelSync", "Lstd/core/String;:Lstd/core/String;", + reinterpret_cast(SecurityLabelAni::GetSecurityLabelSync) }, }; return BindClass(env, className, methods); } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 28c002c7e..9b9c31595 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -43,6 +43,34 @@ function setSecurityLabelSync(path: string, type: DataLevel): void { return SecurityLabelImpl.setSecurityLabelSync(path, type); } +function getSecurityLabel(path: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): string => securitylabelImpl.getSecurityLabelSync(path), path); + promise.then((ret: NullishType): void => { + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function getSecurityLabel(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): string => securitylabelImpl.getSecurityLabelSync(path), path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let r = ret as string; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); +} + +function getSecurityLabelSync(path: string): string { + return securitylabelImpl.getSecurityLabelSync(path); +} + class SecurityLabelImpl { static { @@ -50,4 +78,5 @@ class SecurityLabelImpl { } static native setSecurityLabelSync(path: string, type: DataLevel): void; + static native getSecurityLabelSync(path: string): string; } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index 6c44deb69..ff9ef0806 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -55,6 +55,34 @@ void SecurityLabelAni::SetSecurityLabelSync( } } +ani_string SecurityLabelAni::GetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +{ + auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = DoSetGecurityLabel(srcPath); + if (!ret.IsSuccess()) { + HILOGE("Get securitylabel failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h index 025d65308..8bb5741c9 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h @@ -26,6 +26,7 @@ namespace ANI { class SecurityLabelAni final { public: static void SetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level); + static ani_string GetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); }; } // namespace ANI diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp index fe1c8152a..9a39e068b 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp @@ -37,6 +37,12 @@ FsResult DoSetSecurityLabel(const string &path, const string &dataLevel) return FsResult::Success(); } +FsResult DoSetGecurityLabel(const string &path) +{ + string ret = SecurityLabel::GetSecurityLabel(path); + return FsResult::Success(ret); +} + } // namespace ModuleSecurityLabel } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h index d56c069cb..0caf1b819 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h @@ -24,6 +24,7 @@ namespace ModuleSecurityLabel { using namespace ModuleFileIO; FsResult DoSetSecurityLabel(const string &path, const string &dataLevel); +FsResult DoSetGecurityLabel(const string &path); } // namespace ModuleSecurityLabel } // namespace FileManagement -- Gitee From 99a3ae9a426060cda73fb63a63615a6ed043374b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 24 Mar 2025 17:31:07 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=90=8D=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp | 2 +- interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp | 2 +- interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index ff9ef0806..4d6fd2b32 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -64,7 +64,7 @@ ani_string SecurityLabelAni::GetSecurityLabelSync(ani_env *env, [[maybe_unused]] return nullptr; } - auto ret = DoSetGecurityLabel(srcPath); + auto ret = DoGetSecurityLabel(srcPath); if (!ret.IsSuccess()) { HILOGE("Get securitylabel failed"); const auto &err = ret.GetError(); diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp index 9a39e068b..a438cf033 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp @@ -37,7 +37,7 @@ FsResult DoSetSecurityLabel(const string &path, const string &dataLevel) return FsResult::Success(); } -FsResult DoSetGecurityLabel(const string &path) +FsResult DoGetSecurityLabel(const string &path) { string ret = SecurityLabel::GetSecurityLabel(path); return FsResult::Success(ret); diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h index 0caf1b819..cb7766eaf 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h @@ -24,7 +24,7 @@ namespace ModuleSecurityLabel { using namespace ModuleFileIO; FsResult DoSetSecurityLabel(const string &path, const string &dataLevel); -FsResult DoSetGecurityLabel(const string &path); +FsResult DoGetSecurityLabel(const string &path); } // namespace ModuleSecurityLabel } // namespace FileManagement -- Gitee From 7fb73f94712961e871657421295881bc68a299aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 24 Mar 2025 20:32:18 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp index a438cf033..d68002e8a 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp @@ -40,7 +40,7 @@ FsResult DoSetSecurityLabel(const string &path, const string &dataLevel) FsResult DoGetSecurityLabel(const string &path) { string ret = SecurityLabel::GetSecurityLabel(path); - return FsResult::Success(ret); + return FsResult::Success(move(ret)); } } // namespace ModuleSecurityLabel -- Gitee From a6be1f539467893b7fdfb62c8b7e6bcf7c3a209b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 25 Mar 2025 09:09:43 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 9b9c31595..4bf011871 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -45,7 +45,7 @@ function setSecurityLabelSync(path: string, type: DataLevel): void { function getSecurityLabel(path: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): string => securitylabelImpl.getSecurityLabelSync(path), path); + let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); promise.then((ret: NullishType): void => { let r = ret as string; resolve(r); @@ -56,7 +56,7 @@ function getSecurityLabel(path: string): Promise { } function getSecurityLabel(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): string => securitylabelImpl.getSecurityLabelSync(path), path); + let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -68,7 +68,7 @@ function getSecurityLabel(path: string, callback: AsyncCallback): } function getSecurityLabelSync(path: string): string { - return securitylabelImpl.getSecurityLabelSync(path); + return SecurityLabelImpl.getSecurityLabelSync(path); } class SecurityLabelImpl { -- Gitee From 43c7cbd2b738b43447407d69f9b69a8b05a243c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 26 Mar 2025 10:48:08 +0800 Subject: [PATCH 5/7] =?UTF-8?q?namespace=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../src/mod_hash/ani/bind_function_class.cpp | 2 +- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 40 ++++----- .../ani/bind_function_class.cpp | 2 +- .../ani/ets/@ohos.file.securityLabel.ets | 84 ++++++++++--------- 4 files changed, 66 insertions(+), 62 deletions(-) diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 52ee63589..d93492ac7 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/hash/hashImpl;"; + static const char *className = "L@ohos/file/hash/HashImpl;"; std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 1e68269ac..9ff3885a7 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -15,33 +15,33 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; -function hashSync(path: string, algorithm: string): string { - return HashImpl.hashSync(path, algorithm); -} +namespace hash { + export function hash(path: string, algorithm: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); + promise.then((ret: NullishType): void => { + let res = ret as string; + resolve(res); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } -function hash(path: string, algorithm: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + export function hash(path: string, algorithm: string, callback: AsyncCallback): void { let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType): void => { + promise.then((ret: NullishType) => { + let e = new BusinessError(); + e.code = 0; let res = ret as string; - resolve(res); + callback(e, res); }).catch((e: BusinessError): void => { - reject(e); + callback(e, ""); }); - }); + } } -function hash(path: string, algorithm: string, callback: AsyncCallback): void { - let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - let e = new BusinessError(); - e.code = 0; - let res = ret as string; - callback(e, res); - }).catch((e: BusinessError): void => { - callback(e, ""); - }); -} +export default hash; class HashImpl { diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 47b5daed2..5bb0f2650 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/securityLabel/securitylabelImpl;"; + static const char *className = "L@ohos/file/securityLabel/SecurityLabelImpl;"; std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 4bf011871..b4268c611 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -15,61 +15,65 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; -type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; +namespace securityLabel { + export type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; -function setSecurityLabel(path: string, type: DataLevel): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + export function setSecurityLabel(path: string, type: DataLevel): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { - resolve(undefined); + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); }).catch((e: BusinessError): void => { - reject(e); + callback(e, undefined); }); - }); -} + } -function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); - promise.then((ret: NullishType): void => { - let e = new BusinessError(); - e.code = 0; - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); -} + export function setSecurityLabelSync(path: string, type: DataLevel): void { + return SecurityLabelImpl.setSecurityLabelSync(path, type); + } -function setSecurityLabelSync(path: string, type: DataLevel): void { - return SecurityLabelImpl.setSecurityLabelSync(path, type); -} + export function getSecurityLabel(path: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); + promise.then((ret: NullishType): void => { + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } -function getSecurityLabel(path: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + export function getSecurityLabel(path: string, callback: AsyncCallback): void { let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; let r = ret as string; - resolve(r); + callback(e, r); }).catch((e: BusinessError): void => { - reject(e); + callback(e, ""); }); - }); -} + } -function getSecurityLabel(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); - promise.then((ret: NullishType): void => { - let e = new BusinessError(); - e.code = 0; - let r = ret as string; - callback(e, r); - }).catch((e: BusinessError): void => { - callback(e, ""); - }); + export function getSecurityLabelSync(path: string): string { + return SecurityLabelImpl.getSecurityLabelSync(path); + } } -function getSecurityLabelSync(path: string): string { - return SecurityLabelImpl.getSecurityLabelSync(path); -} +export default securityLabel; class SecurityLabelImpl { @@ -77,6 +81,6 @@ class SecurityLabelImpl { loadLibrary("ani_securitylabel_class"); } - static native setSecurityLabelSync(path: string, type: DataLevel): void; + static native setSecurityLabelSync(path: string, type: securityLabel.DataLevel): void; static native getSecurityLabelSync(path: string): string; } -- Gitee From 76fef00438eaf3699895355c91e24fff20e15808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 26 Mar 2025 10:59:51 +0800 Subject: [PATCH 6/7] =?UTF-8?q?hash=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 9ff3885a7..65c775016 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -15,7 +15,7 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; -namespace hash { +export default namespace hash { export function hash(path: string, algorithm: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); @@ -41,8 +41,6 @@ namespace hash { } } -export default hash; - class HashImpl { static { -- Gitee From 680b90e9d5e685a4f92223bfb33a17bad886e418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 27 Mar 2025 19:54:37 +0800 Subject: [PATCH 7/7] hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 65c775016..3c4fd40dc 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -14,6 +14,7 @@ */ import { BusinessError, AsyncCallback } from '@ohos.base'; +import stream from '@ohos.util.stream'; export default namespace hash { export function hash(path: string, algorithm: string): Promise { @@ -39,6 +40,47 @@ export default namespace hash { callback(e, ""); }); } + + export function createHash(algorithm: string): HashStream { + return new HashStream(algorithm); + } + + export class HashStream extends stream.Transform { + hs: hash.HashStream; + hashBuf?: ArrayBuffer; + + constructor(algorithm: string) { + super(); + this.hs = new hash.HashStream(algorithm); + } + + digest(): string { + return this.hs.digest(); + } + + update(data: ArrayBuffer): void { + this.hs.update(data); + } + + doTransform(chunk: string, encoding: string, callback: () => void): void { + let charCodes: number[] = []; + for (let i = 0; i < chunk.length; i++) { + charCodes = [...charCodes, chunk.charCodeAt(i)]; + } + const buf = new Uint8Array(charCodes).buffer; + this.hs.update((buf as ArrayBuffer)); + this.push(chunk); + callback(); + } + + doWrite(chunk: string | Uint8Array, encoding: string, callback: () => void): void { + callback(); + } + + doFlush(callback: () => void): void { + callback(); + } + } } class HashImpl { -- Gitee