簡體   English   中英

我想在本機應用程序中為給定的視頻生成哈希

[英]I want to generate a hash for a given video, in a react native app

我有一個從React-native App錄制的video文件。 現在我想為這個video文件生成一個數字簽名或哈希值,並將其關聯到區塊鏈。 有什么方法可以為React-native Appvideo文件創建哈希?

您可以使用rnfs直接從存儲中散列文件。

您也可以使用我編寫的一個小包react-native-hash直接從 URL、文件或字符串進行哈希處理,但是,它目前僅適用於 Android。

你可以使用react-native-fetch-blobjs-sha3模塊

將視頻文件編碼為base64 ,您可以使用hash模塊加密base64值。

import RNFetchBlob from 'react-native-fetch-blob'
sha3_256 = require('js-sha3').sha3_256;
...

let data = ''
let hashdata = '';
RNFetchBlob.fs.readStream(
    // file path
    PATH_TO_THE_FILE,
    // encoding, should be one of `base64`, `utf8`, `ascii`
    'base64',
    // (optional) buffer size, default to 4096 (4095 for BASE64 encoded data)
    // when reading file in BASE64 encoding, buffer size must be multiples of 3.
    4095)
.then((ifstream) => {
    ifstream.open()
    ifstream.onData((chunk) => {
      // when encoding is `ascii`, chunk will be an array contains numbers
      // otherwise it will be a string
      data += chunk
    })
    ifstream.onError((err) => {
      console.log('oops', err)
    })
    ifstream.onEnd(() => {  
     hashdata = sha3_256(data); // Convert Data to Hash Value
    })
})

如果使用世博會

import * as DocumentPicker from 'expo-document-picker';
import * as FileSystem from 'expo-file-system';
sha3_256 = require('js-sha3').sha3_256;
const response: IDocumentPickerResponse = await DocumentPicker.getDocumentAsync({
    copyToCacheDirectory: false,
    type: '*/*',
});
const file: string = await FileSystem.readAsStringAsync(
    response.uri,
    {
        encoding: FileSystem.EncodingTypes.Base64,
    });
const hashdata = sha3_256(file); // Convert Data to Hash Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM