static SEC_CHAR * _mongoc_sspi_base64_encode (const SEC_CHAR *value, DWORD vlen) { SEC_CHAR *out = NULL; DWORD len; /* Get the correct size for the out buffer. */ if (CryptBinaryToStringA ((BYTE *) value, vlen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &len)) { out = (SEC_CHAR *) malloc (sizeof (SEC_CHAR) * len); if (out) { /* Encode to the out buffer. */ if (CryptBinaryToStringA ((BYTE *) value, vlen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, out, &len)) { return out; } else { free (out); } } } MONGOC_ERROR ("%s", "CryptBinaryToString failed."); return NULL; }
int ToBase64CryptoA( const BYTE* pSrc, int nLenSrc, char* pDst, int nLenDst ) { DWORD nLenOut= nLenDst; BOOL fRet= CryptBinaryToStringA((const BYTE*)pSrc, nLenSrc,CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,pDst, &nLenOut); if (!fRet) nLenOut=0; return( nLenOut ); }
// OpenSSL HMAC-SHA1 + Base64 エンコーディング TEST( OAuthTest, HmacSha1Base64 ) { size_t resLength; unsigned char res[SHA_DIGEST_LENGTH + 1]; char key[] = "1"; int keyLength = strlen( key ); unsigned char data[] = "012345"; size_t dataLength = sizeof( data ) - 1; HMAC( EVP_sha1(), key, keyLength, data, dataLength, res, &resLength ); char b64[256]; DWORD b64Length; CryptBinaryToStringA( res, resLength, CRYPT_STRING_BASE64, b64, &b64Length ); // 変換した文字列の最後に CRLF がついてくるのでそれをとる。 char b64withoutCRLF[256]; int b64length = strlen( b64 ); for( int i=0; i<b64length-2; i++ ) // CRLF 分が -2 { b64withoutCRLF[i] = b64[i]; } b64withoutCRLF[b64length-2] = 0; // null 文字 // Python で作った値と比較。 EXPECT_STREQ( "uSAq0a63u1W41eTdCrgbywm5CU8=", b64withoutCRLF ); }
HRESULT ToStringEnc(DWORD dwFlags, const BYTE *pbBinary, DWORD cbBinary, LPSTR *pszString) { BOOL bRes = FALSE; HRESULT hRes=S_OK; DWORD cchString = 0; bRes = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, NULL, &cchString); if (!bRes){ dprintf("[sync] send failed at CryptBinaryToString: %d\n", GetLastError()); return E_FAIL; } *pszString = (LPSTR) malloc(cchString); if (*pszString==NULL){ dprintf("[sync] failed at allocate buffer: %d\n", GetLastError()); return E_FAIL; } bRes = CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, *pszString, &cchString); if (!bRes){ dprintf("[sync] failed at CryptBinaryToString: %d\n", GetLastError()); if (*pszString) { free(*pszString); *pszString = NULL; } return E_FAIL; } /* CRYPT_STRING_NOCRLF 0x40000000 Windows Server 2003 and Windows XP: This value is not supported */ #if _NT_TARGET_VERSION_WINXPOR2K3 trimcrlf(*pszString); #endif return hRes; }
std::string CStringUtils::Encrypt(const char * text) { DATA_BLOB blobin = { 0 }; DATA_BLOB blobout = { 0 }; std::string result; blobin.cbData = (DWORD)strlen(text); blobin.pbData = (BYTE*)(LPCSTR)text; if (CryptProtectData(&blobin, L"TSVNAuth", NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &blobout) == FALSE) return result; DWORD dwLen = 0; if (CryptBinaryToStringA(blobout.pbData, blobout.cbData, CRYPT_STRING_HEX | CRYPT_STRING_NOCRLF, NULL, &dwLen) == FALSE) return result; std::unique_ptr<char[]> strOut(new char[dwLen + 1]); if (CryptBinaryToStringA(blobout.pbData, blobout.cbData, CRYPT_STRING_HEX | CRYPT_STRING_NOCRLF, strOut.get(), &dwLen) == FALSE) return result; LocalFree(blobout.pbData); result = strOut.get(); return result; }
HRESULT ToBase64(const BYTE *pbBinary, DWORD cbBinary, LPSTR *pszString) { HRESULT hRes=S_OK; DWORD cchString; hRes = CryptBinaryToStringA(pbBinary, cbBinary, CRYPT_STRING_BASE64|CRYPT_STRING_NOCRLF, NULL, &cchString); if(FAILED(hRes)){ dbgout("[sync] send failed at CryptBinaryToString: %d\n", GetLastError()); return E_FAIL; } *pszString = (LPSTR) malloc(cchString); if (pszString==NULL){ dbgout("[sync] failed at allocate buffer: %d\n", GetLastError()); return E_FAIL; } hRes = CryptBinaryToStringA(pbBinary, cbBinary, CRYPT_STRING_BASE64|CRYPT_STRING_NOCRLF, *pszString, &cchString); if(FAILED(hRes)){ dbgout("[sync] send failed at CryptBinaryToString: %d\n", GetLastError()); return E_FAIL; } /* CRYPT_STRING_NOCRLF 0x40000000 Windows Server 2003 and Windows XP: This value is not supported */ #if _NT_TARGET_VERSION_WINXPOR2K3 trimcrlf(*pszString); #endif return hRes; }