示例#1
0
文件: views.py 项目: BingyZhang/finer
def upload(request, eid=0):
    try:
        e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
        return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        zfile = request.FILES['inputfile']
        sig = request.FILES['sig']
        ## Sanity checks...

        #processing upload files
        z = zipfile.ZipFile(zfile, 'r')
        for name in z.namelist():
            if name.endswith(".txt"):
                opfile = z.read(name)
            else:
                datafile = z.read(name)
        ## Record update log
        notes = timezone.now().isoformat(' ') + "-" + opfile
        zfile.name = notes + ".zip"
        sig.name = "Sig_" + notes + ".txt"
        new_op = UpdateInfo(election=e, text=notes, file=zfile, sig=sig)
        new_op.save()
        flag = 0
        if opfile == 'votecode':
            flag = 1
            reader = datafile.splitlines()
            #populate BBA database handle CSV file myself
            for rows in reader:
                if rows != '':
                    items = rows.split(',')
                    new_entry = Bba(election=e,
                                    serial=items[0].strip().upper(),
                                    code=items[1].strip().upper())
                    new_entry.save()
            return HttpResponse('The votecodes have been uploaded to VBB.')
        elif opfile == 'end':
            flag = 1
            e.end = timezone.now()
            e.save()
            return HttpResponse('The election is ended.')
        elif opfile == 'lock':
            flag = 1
            e.pause = True
            e.save()
            return HttpResponse('The election is locked.')
        elif opfile == 'unlock':
            flag = 1
            e.pause = False
            e.save()
            return HttpResponse('The election is unlocked.')
        if flag == 1:
            return HttpResponse('The data has been uploaded to VBB.')
        else:
            return HttpResponse('Sorry, the operation code is not recognized.')

    else:
        return render_to_response('404.html')
示例#2
0
文件: views.py 项目: BingyZhang/finer
def upload(request, eid = 0):
    try:
	e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
	return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        zfile = request.FILES['inputfile']
        sig = request.FILES['sig']
        ## Sanity checks...

        #processing upload files
	z = zipfile.ZipFile(zfile, 'r')
	for name in z.namelist():
                if name.endswith(".txt"):
			opfile = z.read(name)
		else:
			datafile = z.read(name)
        ## Record update log
	notes = timezone.now().isoformat(' ')+"-"+opfile
	zfile.name = notes+".zip"
	sig.name = "Sig_"+notes+".txt"
        new_op = UpdateInfo(election = e, text = notes, file = zfile, sig = sig)
        new_op.save()
        flag = 0
        if opfile == 'votecode':
                flag = 1 
                reader = datafile.splitlines()
                #populate BBA database handle CSV file myself
                for rows in reader:
                        if rows != '':
                                items = rows.split(',')
                                new_entry = Bba(election = e, serial = items[0].strip().upper(), code = items[1].strip().upper())
                                new_entry.save()
                return HttpResponse('The votecodes have been uploaded to VBB.')                
        elif opfile == 'end':
                flag = 1 
                e.end = timezone.now()
                e.save()
                return HttpResponse('The election is ended.')
        elif opfile == 'lock':
                flag = 1 
                e.pause = True
                e.save()
                return HttpResponse('The election is locked.')
        elif opfile == 'unlock':
                flag = 1 
                e.pause = False
                e.save()
                return HttpResponse('The election is unlocked.')
        if flag == 1:
            return HttpResponse('The data has been uploaded to VBB.')
        else:
            return HttpResponse('Sorry, the operation code is not recognized.')                
        
        
    else:
        return render_to_response('404.html')
示例#3
0
文件: views.py 项目: BingyZhang/finer
def upload(request, eid = 0):
    try:
	e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
	return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        csvfile = request.FILES['inputfile']
	reader = csvfile.read().splitlines()
	counter = -2
	serial = ""
	enc1 = ""
	enc2 = ""
	cipher1 = ""
	cipher2 = ""
	plain1 = ""
	plain2 = ""
	code1 = []
	code2 = []
	n = 0
	key = ""
	for temp in reader:
	    counter+=1
	    #first row n, k1
	    if counter ==-1:
		row = temp.split(',')
	        n = int(row[0])
		key = row[1]
	        new_r = Randomstate(election = e, notes = "k1",random = key)
	        new_r.save()
	    if counter%7 ==0:
		#serial key
		row = temp.split(',')
		serial = row[0]
		new_bba = Bba(election = e, serial = serial, key = row[1], n = n)
		new_bba.save()
	    if	counter%7 ==1:
		#enc1
		enc1 = temp
		row = temp.split(',')
		k = base64.b64decode(key)
		for item in row:
		    code1.append(decrypt(base64.b64decode(item),k))
            if  counter%7 ==2:
                #cipher1
                cipher1 = temp
            if  counter%7 ==3:
                #plain1
                plain1 = temp
            if  counter%7 ==4:
                #enc2
                enc2 = temp    
                row = temp.split(',')
                k = base64.b64decode(key)
                for item in row:
                    code2.append(decrypt(base64.b64decode(item),k))
            if  counter%7 ==5:
                #cipher2
                cipher2 = temp
            if  counter > 0 and counter%7 ==6:
                #cipher2
                plain2 = temp

		#random aux
		fake_aux = []
		fake_rand = []
		for i in range(n):
			fake_aux.append(base64.b64encode(os.urandom(16)))
			fake_rand.append(base64.b64encode(os.urandom(16)))
		temp1 = ",".join(fake_aux)
		temp2 = ",".join(fake_rand)
		new_abb = Abbinit(election = e, codes1 = ",".join(code1), codes2 = ",".join(code2), rand1 = temp2, rand2 = temp2 , aux1 = temp1, aux2 = temp1, zeroone = base64.b64encode(os.urandom(8)),serial = serial, enc1 = enc1, enc2 = enc2, cipher1 = cipher1, cipher2 = cipher2,plain1 = plain1, plain2 = plain2)
		new_abb.save()
		#clean var
		code1 = []
        	code2 = []
	e.prepared = True
	e.save()
	return HttpResponse("Success")

    else:
        return render_to_response('404.html')
示例#4
0
def upload(request, eid=0):
    try:
        e = Election.objects.get(EID=eid)
    except Election.DoesNotExist:
        return HttpResponse('The election ID is invalid!')
    if request.method == 'POST':
        csvfile = request.FILES['inputfile']
        reader = csvfile.read().splitlines()
        counter = -2
        serial = ""
        enc1 = ""
        enc2 = ""
        cipher1 = ""
        cipher2 = ""
        plain1 = ""
        plain2 = ""
        code1 = []
        code2 = []
        n = 0
        key = ""
        for temp in reader:
            counter += 1
            #first row n, k1
            if counter == -1:
                row = temp.split(',')
                n = int(row[0])
                key = row[1]
                new_r = Randomstate(election=e, notes="k1", random=key)
                new_r.save()
            if counter % 7 == 0:
                #serial key
                row = temp.split(',')
                serial = row[0]
                new_bba = Bba(election=e, serial=serial, key=row[1], n=n)
                new_bba.save()
            if counter % 7 == 1:
                #enc1
                enc1 = temp
                row = temp.split(',')
                k = base64.b64decode(key)
                for item in row:
                    code1.append(decrypt(base64.b64decode(item), k))
            if counter % 7 == 2:
                #cipher1
                cipher1 = temp
            if counter % 7 == 3:
                #plain1
                plain1 = temp
            if counter % 7 == 4:
                #enc2
                enc2 = temp
                row = temp.split(',')
                k = base64.b64decode(key)
                for item in row:
                    code2.append(decrypt(base64.b64decode(item), k))
            if counter % 7 == 5:
                #cipher2
                cipher2 = temp
            if counter > 0 and counter % 7 == 6:
                #cipher2
                plain2 = temp

                #random aux
                fake_aux = []
                fake_rand = []
                for i in range(n):
                    fake_aux.append(base64.b64encode(os.urandom(16)))
                    fake_rand.append(base64.b64encode(os.urandom(16)))
                temp1 = ",".join(fake_aux)
                temp2 = ",".join(fake_rand)
                new_abb = Abbinit(election=e,
                                  codes1=",".join(code1),
                                  codes2=",".join(code2),
                                  rand1=temp2,
                                  rand2=temp2,
                                  aux1=temp1,
                                  aux2=temp1,
                                  zeroone=base64.b64encode(os.urandom(8)),
                                  serial=serial,
                                  enc1=enc1,
                                  enc2=enc2,
                                  cipher1=cipher1,
                                  cipher2=cipher2,
                                  plain1=plain1,
                                  plain2=plain2)
                new_abb.save()
                #clean var
                code1 = []
                code2 = []
        e.prepared = True
        e.save()
        return HttpResponse("Success")

    else:
        return render_to_response('404.html')