[작은 작품] 내장 형 카메라 방문 스마트 캡 처 시스템 (MD5 암호 화 포함)

10561 단어 MD5 암호 화

저자: 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/

좋은 웹페이지 즐겨찾기