Добавлена возможность указания кодировки входящего сообщения в метод createHash

Signed-off-by: vgoma <vgoma@yandex.ru>
This commit is contained in:
Alexey 2023-06-22 06:38:22 +03:00 committed by vgoma
parent fb28c06cb1
commit 498301e4e9
2 changed files with 19 additions and 3 deletions

View File

@ -41,4 +41,10 @@ describe('createHash', () => {
expect(hash).toEqual('hash'); expect(hash).toEqual('hash');
}); });
test('returns created hash with specified encoding', async () => {
const hash = await createHash('message', { encoding: 'binary' });
expect(hash).toEqual('hash');
});
}); });

View File

@ -1,7 +1,13 @@
import { TranscodeEncoding } from 'buffer';
import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded'; import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded';
import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage'; import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage';
import { __cadesAsyncToken__, __createCadesPluginObject__, _generateCadesFn } from '../helpers/_generateCadesFn'; import { __cadesAsyncToken__, __createCadesPluginObject__, _generateCadesFn } from '../helpers/_generateCadesFn';
type Options = {
hashedAlgorithm?: number;
encoding?: TranscodeEncoding;
};
/** /**
* Создает хеш сообщения по ГОСТ Р 34.11-2012 (по умолчанию 256 бит) * Создает хеш сообщения по ГОСТ Р 34.11-2012 (по умолчанию 256 бит)
* https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D1%80%D0%B8%D0%B1%D0%BE%D0%B3_(%D1%85%D0%B5%D1%88-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8F) * https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D1%80%D0%B8%D0%B1%D0%BE%D0%B3_(%D1%85%D0%B5%D1%88-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8F)
@ -12,7 +18,7 @@ import { __cadesAsyncToken__, __createCadesPluginObject__, _generateCadesFn } fr
* @returns хеш * @returns хеш
*/ */
export const createHash = _afterPluginsLoaded( export const createHash = _afterPluginsLoaded(
async (unencryptedMessage: string | ArrayBuffer, hashedAlgorithm?: number): Promise<string> => { async (unencryptedMessage: string | ArrayBuffer, options?: Options): Promise<string> => {
const { cadesplugin } = window; const { cadesplugin } = window;
return eval( return eval(
@ -22,7 +28,11 @@ export const createHash = _afterPluginsLoaded(
let hash; let hash;
try { try {
if (options?.encoding && typeof unencryptedMessage === 'string') {
messageBase64 = Buffer.from(unencryptedMessage, options?.encoding).toString('base64');
} else {
messageBase64 = Buffer.from(unencryptedMessage).toString('base64'); messageBase64 = Buffer.from(unencryptedMessage).toString('base64');
}
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -33,7 +43,7 @@ export const createHash = _afterPluginsLoaded(
void ( void (
__cadesAsyncToken__ + __cadesAsyncToken__ +
cadesHashedData.propset_Algorithm( cadesHashedData.propset_Algorithm(
hashedAlgorithm ?? cadesplugin.CADESCOM_HASH_ALGORITHM_CP_GOST_3411_2012_256, options?.hashedAlgorithm ?? cadesplugin.CADESCOM_HASH_ALGORITHM_CP_GOST_3411_2012_256,
) )
); );
void (__cadesAsyncToken__ + cadesHashedData.propset_DataEncoding(cadesplugin.CADESCOM_BASE64_TO_BINARY)); void (__cadesAsyncToken__ + cadesHashedData.propset_DataEncoding(cadesplugin.CADESCOM_BASE64_TO_BINARY));