【小作品】嵌入式上的摄像头房门智能捕捉系统(含MD5加密)

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ #include #include #include #include "spcaframe.

 

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "spcaframe.h"
#include "spcav4l.h"

#include "xyssl/rsa.h"
#include "xyssl/sha1.h"

char rsa_N[]    = "807E3526556FADF8D4CA64074ADA36862646D5ECB24E363821306588722AF2B58058CFB88E8C0BEA5C7084F3055D232F110E59C8837A0D132A4B907E91DB4A49"
                  "24134A85E7445935E55A772C0B72E12C94501D9DF66B71BA030F842531721AEF43AE48F9505BF7504CDEEA3CAA6F94530835648D770AE2E6C628DD484D10AA57";
char rsa_E[]    = "010001";
char rsa_D[]    = "56B3D2AD612D10993D0CAC5E7755B340E6071A46B3322F47C4AD6175A683F06E2482C8F761C88229CBE268F38B0503BEB8A59453C6D3CE8AC6196310E4DEB1CA"
                  "939DF7F7EE26C4697EEDD1E5122795BFC83861DE2E3EC9E3E84F42B3A9DD25EB09B30FDDFFACCE5091493BC5577530CE9CD9C8BA244EC5FD3DF91BCECFD73961";
char rsa_P[]    = "F8DAD6A5651CED9011D979A076D70C4FBD095AAE2E53EF51415832C63AD61618F0BB369F29D1363345FE481FE6C28F0830FE33A1C41F8743A4E02DD682A2E099";
char rsa_Q[]    = "842EABF3171F972DE7D6B571B70F969F8F1C305851785BB042CDAE3B794014659A744EA7D16D881B7168463CEEAF52BA0F78755BBE89CFE1361076CE3E20886F";
char rsa_DP[]    = "B1C694047FE1548CD1538D21E703E595A933DF86032E8F0E7B21E8D3D8004CB4F074ADA6B296F4A35863395F20D8E8992F76C9A7CC95C169BF852EF9C9455631";
char rsa_DQ[]    = "143C54E49D289FEB4E2FC78D461A23D3FF83B03F0511E8EF7DFAA0EEC7EC3073318716B7884F3D63FE239985208144A7E950669F09F76D14AC432EFCF9F3DF0F";
char rsa_QP[]    = "C2F98F412476BDA2B14F5882D929090C62BB24ED74E8B78A3BE287EABDB3FADC445D041F1DE04EBE2D39A8913DAF03C23FF632D1B3FB6CCBDD65B2A576F127F5";

char videodevice[]   =   "/dev/video0";
int  format          =  VIDEO_PALETTE_YUV420P;
int  width           =    640;
int  height          =    480;
int  grabmethod      =  1;      //  1 = mmap    0   =   read

int     picNum;

struct vdIn videoIn  =  {0};

#define MY_BUFSIZE  (((640*480)*3)>>1)

#define DETECT_INTERVAL 100
#define SAMPLE_TIMES    10
#define SKIP_FIRST_FEW  8

#define CATPURE_SEQ     3

#define RESAMPLE_TIMES  200
#define CONTIUOUS_TIMES 8

char fBuf[2][MY_BUFSIZE];
int  curBuf;
int  prevBuf;
int     hasInit;
int  testTimesRemain;
int  maxLevel;
int     skipStart;
int  contTimes;  // Continuous picking times
int  pickTimes;
int  capSeq;

struct timespec sleepTime;

void grabCmp(void);
void WritePic (unsigned char *src, int size);

int
main (void)
{
  int i,j;

  if (init_videoIn(&videoIn, videodevice, width, height, format,grabmethod) != 0)     
        printf ("Can't Init Device!/n");
    
    hasInit            = 0;                // Identify that the prev frame is valid
    testTimesRemain    = SAMPLE_TIMES;        //Sample Times
    skipStart        = SKIP_FIRST_FEW;                //Skip First few frame
    maxLevel        = 0;
    contTimes        = 0;
    pickTimes        = 0;
    prevBuf            = 0;
    curBuf            = 1;
    picNum            = 0;

    i = DETECT_INTERVAL/1000;
    j = (DETECT_INTERVAL%1000) * 1000 * 1000;

     sleepTime.tv_sec  = i;
     sleepTime.tv_nsec = j;
  
    for(;;)
    {
        grabCmp(); 
        nanosleep(&sleepTime,NULL);
    }
    return 0;
}

void
grabCmp (void)
{
    unsigned char *pictureData =NULL;
    struct frame_t *headerframe;

    int diff,tmpDiff;
    int    i;
    
    videoIn.vmmap.height    = videoIn.hdrheight;
    videoIn.vmmap.width        = videoIn.hdrwidth;
    videoIn.vmmap.format    = videoIn.formatIn;
    if (ioctl (videoIn.fd, VIDIOCSYNC,&videoIn.vmmap.frame) < 0)
        printf ("cvsync err/n");
        
    memcpy(    fBuf[curBuf],
            videoIn.pFramebuffer + videoIn.videombuf.offsets[videoIn.vmmap.frame],
            MY_BUFSIZE);    // Copy Data Out

    if ((ioctl (videoIn.fd, VIDIOCMCAPTURE, &(videoIn.vmmap))) < 0)
        printf ("cmcapture");

    videoIn.vmmap.frame = (videoIn.vmmap.frame + 1) % videoIn.videombuf.frames;        
        
    if(hasInit)        // Check if prev frame is valid
    {
        // Do some stuff in some special conditions
        if(skipStart)
        {
            skipStart --;    // Skip First Few Frame
            curBuf    =    (curBuf+1)%2;
            prevBuf =    (prevBuf+1)%2;
            return;
        }
        if(pickTimes >= RESAMPLE_TIMES)
        {
            pickTimes = 0;
            testTimesRemain = SAMPLE_TIMES;
            printf("Running Time Limit Reached,Resampling.../n");
        }

        
        // Now,We Began to compare
        diff = 0;
        for(i = 0;i<MY_BUFSIZE;i++)
        {
            tmpDiff = fBuf[curBuf][i] - fBuf[prevBuf][i];
            if(tmpDiff > 0)
                diff += tmpDiff;
            else
                diff -= tmpDiff;
        }

        if(testTimesRemain)
        {
            if(testTimesRemain == SAMPLE_TIMES)
                printf("Now we begin to pick some samples.Please wait .../n");
                
            if(diff > maxLevel)
                maxLevel = diff;
            printf("Sample %d: %d/n",SAMPLE_TIMES - testTimesRemain + 1,diff);
            
            if(testTimesRemain == 1)
            {
                maxLevel    *=    1.1;        //We mul a constant for fault-tolerance
                printf("Sampling Done! Maximum Level: %d/n",maxLevel);
            }
            
            testTimesRemain--;
        }
        else
        {
            // We Print some debug infomation 
            printf("Cur: %d Prev: %d Diff %d/n",curBuf,prevBuf,diff);

            if(diff > maxLevel || capSeq)
            {
                int jpegsize;

                jpegsize =    convertframe(videoIn.ptframe[0]+ sizeof(struct frame_t),
                                         fBuf[curBuf],
                                         width,height,videoIn.formatIn,1024);

                WritePic(videoIn.ptframe[0]+ sizeof(struct frame_t),jpegsize);

                hasInit =    0;
                if(capSeq)
                    capSeq--;
                else
                {
                    capSeq  = CATPURE_SEQ;
                    contTimes++;
                    if(contTimes >= CONTIUOUS_TIMES)
                    {
                        contTimes = 0;
                        testTimesRemain = SAMPLE_TIMES;
                        printf("Capture Too Frequently,ReSampling.../n"); 
                    }
                }
            }
            else
            {
                contTimes = 0;
            }
        }
    }
    else
    {
        hasInit    = 1;
    }
    pickTimes++;
    curBuf    =    (curBuf+1)%2;
    prevBuf =    (prevBuf+1)%2;
}


void
WritePic (unsigned char *src, int size)
{
    FILE *foutpict;
    static char filename[40];
    int i,ret;

    time_t curdate;
    struct tm *tdate;
    
    rsa_context rsa;
    unsigned char hash[20];
    unsigned char rsabuf[512];
    
    for ( i= 1024 ; i< size; i++)
        if ((src[i] == 0xFF) && (src[i+1] == 0xD9))
            break;
    i+=10;    //Get Jpeg size
    

    memset (filename, 0, sizeof (filename));
    time (&curdate);
    tdate = localtime (&curdate);
    snprintf (filename, 40, "%02d:%02d:%04d-%02d:%02d:%02d-P%04d.jpg",
        tdate->tm_mon + 1, tdate->tm_mday, tdate->tm_year + 1900,
        tdate->tm_hour, tdate->tm_min, tdate->tm_sec, picNum++);

      foutpict = fopen(filename, "wb");
    fwrite (src, sizeof (char), i, foutpict);
    fclose (foutpict);

    //Now We Create a Signature
    sha1(src,i,hash);

    rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );

    mpi_read_string( &rsa.N , 16, rsa_N );
    mpi_read_string( &rsa.E , 16, rsa_E );
    mpi_read_string( &rsa.D , 16, rsa_D );
    mpi_read_string( &rsa.P , 16, rsa_P );
    mpi_read_string( &rsa.Q , 16, rsa_Q );
    mpi_read_string( &rsa.DP, 16, rsa_DP );
    mpi_read_string( &rsa.DQ, 16, rsa_DQ );
    mpi_read_string( &rsa.QP, 16, rsa_QP );

    rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;

    if(rsa_pkcs1_sign(&rsa,RSA_PRIVATE,RSA_SHA1,20,hash,rsabuf) == 0)
    {
        memcpy(filename + strlen(filename),".sig",5);

        foutpict = fopen(filename, "wb");
        for( i = 0; i < rsa.len; i++ )
            fprintf( foutpict, "%02X%s", rsabuf[i],
                     ( i + 1 ) % 16 == 0 ? "/r/n" : " " );
        fclose (foutpict);
    filename[strlen(filename)-4]  = 0;
        printf ("Captured One Picture,Filename: %s/n", filename);
    }
    else
        printf ("Captured One Picture,but failed to generate a signature,Filename: %s/n", filename);

}

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
2月前
|
存储 安全 5G
|
4月前
|
存储 NoSQL 数据库
认证服务---整合短信验证码,用户注册和登录 ,密码采用MD5加密存储 【二】
这篇文章讲述了在分布式微服务系统中添加用户注册和登录功能的过程,重点介绍了用户注册时通过远程服务调用第三方服务获取短信验证码、使用Redis进行验证码校验、对密码进行MD5加密后存储到数据库,以及用户登录时的远程服务调用和密码匹配校验的实现细节。
认证服务---整合短信验证码,用户注册和登录 ,密码采用MD5加密存储 【二】
|
3月前
|
数据安全/隐私保护 Python
Python中的MD5加密“解密”
Python中的MD5加密“解密”
64 0
|
9天前
|
前端开发 数据安全/隐私保护
全新紫色新UI数码盲盒系统源码/全开源无加密/附教程
全新紫色新UI数码盲盒系统源码/全开源无加密/附教程 前端uniapp+H5 后端FastAdmin框架 可打包成APP多端运行 亲测可用
34 13
|
2月前
|
存储 Java 数据库
密码专辑:对密码加盐加密,对密码进行md5加密,封装成密码工具类
这篇文章介绍了如何在Java中通过加盐和加密算法(如MD5和SHA)安全地存储密码,并提供了一个密码工具类PasswordUtils和密码编码类PasswordEncoder的实现示例。
41 10
密码专辑:对密码加盐加密,对密码进行md5加密,封装成密码工具类
|
2月前
|
NoSQL Java Redis
shiro学习四:使用springboot整合shiro,正常的企业级后端开发shiro认证鉴权流程。使用redis做token的过滤。md5做密码的加密。
这篇文章介绍了如何使用Spring Boot整合Apache Shiro框架进行后端开发,包括认证和授权流程,并使用Redis存储Token以及MD5加密用户密码。
36 0
shiro学习四:使用springboot整合shiro,正常的企业级后端开发shiro认证鉴权流程。使用redis做token的过滤。md5做密码的加密。
|
2月前
|
机器学习/深度学习 人工智能 安全
智能时代的隐私守护者:AI加密技术的崛起与挑战###
本文深入探讨了人工智能(AI)在数据加密领域的创新应用,分析了AI如何增强数据安全性,同时也指出了面临的挑战和未来发展趋势。通过具体案例分析,展现了AI加密技术在保护个人隐私与促进数据安全方面的潜力,为读者提供对未来智能时代隐私保护的深刻洞见。 ###
|
2月前
|
数据安全/隐私保护 Python
Python中的MD5加密“解密”
Python中的MD5加密“解密”
103 0
|
2月前
|
IDE 开发工具 数据安全/隐私保护
Python编程实现批量md5加密pdf文件
Python编程实现批量md5加密pdf文件
39 0
|
7天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将介绍网络安全的重要性,分析常见的网络安全漏洞及其危害,探讨加密技术在保障网络安全中的作用,并强调提高安全意识的必要性。通过本文的学习,读者将了解网络安全的基本概念和应对策略,提升个人和组织的网络安全防护能力。