繁体   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