#INCLUDE ONCE "WinCrypt.inc
'//-----------------------------------------------------------------------
'// 生成SHA256密码 32位
'//-----------------------------------------------------------------------
FUNCTION SHA256_ComputeHash(BYVAL pass AS STRING) AS STRING
LOCAL i AS LONG
LOCAL VersionInformation AS OSVERSIONINFOW
LOCAL CSP_NAME AS STRING
LOCAL hCryptProv AS DWORD
LOCAL hCryptHash AS DWORD
LOCAL lnHashSize AS DWORD
LOCAL lnStatus AS LONG
LOCAL HashLen AS DWORD
LOCAL p AS BYTE PTR
LOCAL sha256 AS STRING
GetVersionEx(VersionInformation)
CSP_NAME = $$MS_ENH_RSA_AES_PROV_XP_W
IF VersionInformation.dwMajorVersion<>5 THEN
CSP_NAME = $$MS_ENH_RSA_AES_PROV_W
END IF
'// 获得指定CSP的密钥容器的句柄
lnStatus = CryptAcquireContext(hCryptProv, BYVAL %NULL, MS_ENH_RSA_AES_PROV, %PROV_RSA_AES, %CRYPT_VERIFYCONTEXT)
IF(lnStatus<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//创建一个HASH对象, 指定HASH算法
lnStatus = CryptCreateHash(hCryptProv, %CALG_SHA_256, BYVAL %NULL, BYVAL %NULL, hCryptHash)
IF(lnStatus<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//计算HASH数据
lnStatus = CryptHashData(hCryptHash, BYVAL STRPTR(pass), LEN(pass), 0)
IF(lnStatus<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//获取HASH结果的大小
lnHashSize = LEN(pass)
lnStatus = CryptGetHashParam(hCryptHash, %HP_HASHSIZE, HashLen, lnHashSize, 0) '获取哈希值缓存大小
IF(lnStatus<>1 OR HashLen=0) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
p = VARPTR(HashLen)
HashLen = @p
DIM buf(HashLen-1) AS BYTE '分配缓存空间
'//获取HASH结果
lnStatus = CryptGetHashParam(hCryptHash, %HP_HASHVAL, BYVAL VARPTR(buf(0)), HashLen, 0)
IF(lnStatus<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
FOR i=0 TO UBOUND(buf)
sha256 = sha256 + CHR$(buf(i))
NEXT i
ERASE buf()
IF(hCryptHash>0) THEN CryptDestroyHash(hCryptHash)
IF(hCryptProv>0) THEN CryptReleaseContext(hCryptProv, 0)
FUNCTION = sha256
END FUNCTION
PowerBasic 生成SHA256密码 32位
于 2022-09-24 12:16:04 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)