Sunday, January 17, 2016

Python and Swift Boolean Algebra

After spending a number of years under heavy travel from work I decided to leave Cisco and work for a local partner that resells primarily Cisco equipment.  This has given me more time at home and thus more time to do the things I enjoy like programming.

I've been doing a lot of Python lately because it maps well to work related project like Cisco ACI.  I've also spent a bit of time learning about Swift and really like how it's fairly easy to switch between the two even with my limited programming capacity.

Anyway, on to the Boolean Algebra.  I have implemented functions of this a few times when checking state for various programs, but didn't know it had a cool name like Boolean Algebra.  While writing some code this morning in I came across this as one of the project and thought I would do it in swift also just to see if it was easy to port the logic between the two as well as learn the other operators.

Below you will find two examples, the first in Python and the second in Swift (playground).  I've done them slightly different in how I'm testing the outcome while still trying to keep it very simple.  It's very possible to write these without the if statements by using a dictionary and it probably what you should do if you are going to put this entire thing in real code but I'm not here to tell you how to write code, just give you the answers on calculate Boolean Algebra.

#!/usr/bin/env python
operations = ("conjunction", "disjunction", "implication", "exclusive", "equivalence")

def booleanArith (operation, x, y):
    if operation == "conjunction":
        return x & y

    if operation == "disjunction":
        return (x or y)
    if operation == "implication":
        return (not(x) or y)
    if operation == "exclusive":
        return x != y
    if operation == "equivalence":
        return x == y

def runThem(x, y):
    for operation in operations:
        result = (booleanArith(operation, x, y))
        print ("{} - {},  {} == {}".format(operation, x, y, result))
    print ("")

# Testing code
x = False
y = False
runThem(x, y)

x = True
y = False
runThem(x, y)

x = False
y = True
runThem(x, y)

x = True
y = True
runThem(x, y)

And then Swift:
//: Playground - noun: a place where people can play

func booleanArith(operation: String, x: Bool, y: Bool) -> Bool {
    if operation == "conjunction" {
    return (x && y)
    if operation == "disjunction"{
    return (x || y)
    if operation == "implication"{
    return !x || y
    if operation == "exclusive"{
    return x != y
    if operation == "equivalence"{
    return x == y
    return false

// Testing code

print (booleanArith("conjunction", x: false, y: false))
print (booleanArith("disjunction", x: false, y: false))
print (booleanArith("implication", x: false, y: false))
print (booleanArith("exclusive",   x: false, y: false))
print (booleanArith("equivalence", x: false, y: false))

print (booleanArith("conjunction", x: true, y: false))
print (booleanArith("disjunction", x: true, y: false))
print (booleanArith("implication", x: true, y: false))
print (booleanArith("exclusive",   x: true, y: false))
print (booleanArith("equivalence", x: true, y: false))

print (booleanArith("conjunction", x: false, y: true))
print (booleanArith("disjunction", x: false, y: true))
print (booleanArith("implication", x: false, y: true))
print (booleanArith("exclusive",   x: false, y: true))
print (booleanArith("equivalence", x: false, y: true))

print (booleanArith("conjunction", x: true, y: true))
print (booleanArith("disjunction", x: true, y: true))
print (booleanArith("implication", x: true, y: true))
print (booleanArith("exclusive", x: true, y: true))
print (booleanArith("equivalence", x: true, y: true))

Monday, January 17, 2011

Apple iTunes export restrictions on apps

I recently went through the process of building an app for the iTunes app store that used SSL (HTTPS) encryption.  While this seems trivial, it's not.  Depending on where you go, and what you read you might think you don't need to do anything.  That's wrong.  If you application uses encryption (including SSL and HTTPS, possible other public domain encryption) then you need to get at least get an ERN before submitting your app to Apple for approval.

I believe there was some rule changes in May 2010 that made this possible.  Prior to these changes, you would have had to get a CCATS for your application.  I originally did the full CCATS process, but it was not approved, and they in their own words told me to use this method.

Here is what I did.

Read all the steps before you do this:
1.  Go to this link and use his instructions.  This is a great post:  (thank you jcesar)
2. Do step 1 and 2 for all cases.  If you built your own encryption mechanism, that follow the entire post.  If you used SSL or other public domain encryption, then you can stop after you have your SNAP-R account.
3.  Go to the SNAP-R login site and login:
4.  Click "Create Work Item"
5.  Select "Encryption Registration"

6.  Most if it will be filled out for you.  Add this information to the "Additional Information" field.  I made this up, so your milage will vary.

7. Then attach a document that looks like this one for Encryption Registration Supplement No. 5 to Part 742
Control Policy—CCL Based Controls Supplement No. 5 to Part 742--page 1
Export Administration Regulations June 25, 2010
Certain classification requests and self-classification reports for encryption items must be supported by an encryption registration, i.e., the information as described in this Supplement, submitted as a support documentation attachment to an application in accordance with the procedures described in §§ 740.17(b), 740.17(d), 742.15(b), 748.1, 748.3 and Supplement No. 2 to part 748 of the EAR.
(1) Point of Contact Information
(a) Contact Person
Tige Phillips
(b) Telephone Number
(c) Fax Number
(d) E-mail address
(e) Mailing Address
xxxx E. General Road 
Somewhere, OR 9xxxx
(2) Company Overview (approximately 100 words)
I am an individual developer of software.  The software I create typically falls into one of two categories: Business related, where it would interact with systems created by Cisco Systems, or Recreation software that would be simple fun programs for use by individuals.  For development I use openly available development platforms from Apple Computers and other manufactures.  Creating software is a hobby, not my primary profession.
(3) Identify which of the following categories apply to your company’s technology/families of products:
(a) Wireless
(i) 3G cellular
(ii) 4G cellular/WiMax/LTE
(iii) Short-range wireless / WLAN
(iv) Satellite
(v) Radios
(vi) Mobile communications, n.e.s.
(b) Mobile applications
(c) Computing platforms
(d) Multimedia over IP
(e) Trusted computing
(f) Network infrastructure
(g) Link layer encryption
(h) Smartcards or other identity management
(i) Computer or network forensics
(j) Software
Yes:  I only create software.
(i) Operating systems
(ii) Applications
(k) Toolkits / ASICs / components
(l) Information security including secure storage
(m) Gaming
(n) Cryptanalytic tools
(o) “Open cryptographic interface” (or other support for user-supplied or non-standard cryptography)
(p) Other (identify any not listed above)
(q) Not Applicable (Not a producer of encryption or information technology items)
(4) Describe whether the products incorporate or use proprietary, unpublished or non-standard cryptographic functionality, including encryption algorithms or protocols that have not been adopted or approved by a duly recognized international standards body. (If unsure, please explain)
My products do not use any proprietary, unpublished or non-standard cryptographic functionality.  I only use standards based encryption that can be found on the Internet.  Standards based Secure Socket Layer (SSL) encryption is an example of what I use.  I also only use encryption mechanisms that are available in development platforms by companies like Apple Computers.
(5) Will your company be exporting “encryption source code”?
(6) Do the products incorporate encryption components produced or furnished by non-U.S. sources or vendors? (If unsure, please explain)
(7) With respect to your company’s encryption products, are any of them manufactured outside the United States? If yes, provide manufacturing locations. (Insert “not applicable”, if you are not the principal producer of encryption products) 
8. Once you hit submit, you will get a message in your message box.

9.  That message with have your ERN (Encryption Registration Number) in it.  
10. Open the message and take a screen shot of the message.

11.  Go submit your app to the app store.  When they ask about encryption, tell them.  If you need to submit an ERN, you have one.  :)  I gave them a word document with the screen shot of the ERN, and a very brief explanation.

You're done.  The first status your app will go through is "Waiting for export compliance".  Once your app is approved you will have a LEGAL app on the app store and you didn't have to lie to Apple or the US government.

Monday, December 27, 2010

Update to ATV2 problem - Fixed

After a few more hours of troubleshooting and reading I found there is a feature in DNSMasq that protects you from a DNS Rebind attack.  Unfortunately, the way Netflix works it uses something that looks just like that, so your DNS queries don't work and you get no video.

With most setups you could change the DNS server of your Apple TV (and iPhones, because this happens on them also) to the google address (  Of you can look at your router to see what your ISPs DNS server is and use that.

If you are using DD-WRT, you can upgrade (or look, you might already have this feature if you upgraded in the last 2 months) to a version that has "No DNS Rebind" on the services page in the DNSMasq section.  This turns off that security feature.  Below is a picture with it turned off.  Be sure to reboot your router after you make this change.

If you are using DNSMasq on something else, look into a way to turn off DNS Rebind protection.  There are several commands out there for Linux based systems, but I don't have the time to go check them.  You can start at this link that has some of the commands: 

Sunday, December 26, 2010

Apple TV 2 problems

I've been working on the "no netflix videos" for a few days now, and finally tried something new and got it working.  Prior to this I could browse everything, but trying to play would spin the pinwheel, and then go back to the movie description window.  Now everything works great and I can stream movies and TV shows from Netflix.

Here is what I did.
1.  I connected my ATV2 directly to the back of my cable modem.  This worked.
Going with the idea that maybe there is a connection that is being started from Netflix I decided to try normal port forwarding things on the router.
2.  I setup UPNP.  That didn't work.
3.  I setup a DMZ (basically it port forwards all incoming new connections) to the ATV2.  That didn't work either.

My final setup now until Apple/Netflix/Me figure out what's wrong and fix it is the following:
I used an old Ethernet switch, and have the these thigns connected to it:
1.  Cable modem
2.  Linksys E3000 - WAN Port (my main Internet router for my house with lots of stuff behind it)
3.  ATV2 Ethernet port

The big difference in this setup is that the ATV2 has a public Internet address as opposed to a private address.  If you have more than one Internet device in your house, you are probably using private addressing.  Your ATV probably has an address that starts with 192.168.x.x where x any number between 1 and 254.

Wednesday, May 12, 2010

How to know in what row an accessory was clicked - TableView

I was creating a grouped tableview that had detail disclosures on some of the cells that should then open a new view and make some changes. I'm using the following code to figure out what cell the accessory is in.

The key things to remember are the "accessoryButtonTappedForRowWithIndexPath" method, and then using the switch (indexPath.section) for the section and switch (indexPath.row) for the row.

The trick I used was the copy the "switch" information directly from the method I used to build the table in the first place. I took out the logic I used, but left in a comment to help me remember what line was what. example: // Description

Happy coding!

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

ServerDescriptionView *serverDescriptionView = [[ServerDescriptionView alloc] initWithNibName:@"ServerDescriptionView" bundle:nil];

serverDescriptionView.profile = profile.agencyPolicyName;

ServerActiveServerView *serverActiveServerView = [[ServerActiveServerView alloc] initWithNibName:@"ServerActiveServerView" bundle:nil];

serverActiveServerView.profile = profile.agencyPolicyName;

switch (indexPath.section) {

case 1:

switch (indexPath.row) {

case 0:

// Description

[[self navigationController] pushViewController:serverDescriptionView animated:YES];

[serverDescriptionView release];


case 1:

// Active Server

[[self navigationController] pushViewController:serverActiveServerView animated:YES];

[serverActiveServerView release];




case 2:

switch (indexPath.row) {

case 0:

// Server Power


case 1:

// Unbind from the Template


case 2:

// Reset UUID





Tuesday, May 11, 2010

Calling methods in another class file

Now that I have my app doing quite a few things I'm starting to realize that I should have created one central place to put a bunch of functions and then call them from different views. So, I spent some time in google (because I don't have any iPhone programming books, nor do I know how to program object oriented stuff) and found a few different way to do this. Actually, they are just variations of the same thing. I'll show how I'm doing it in my code now.

In the YourFunctions.h file of the new class:

- (void) showAlert:(int)errorNumber;

In the YourFunctions.m file of the new class:

- (void) showAlert:(int)errorNumber {

UIAlertView *error = [[UIAlertView alloc] initWithTitle:[[NSString alloc] initWithFormat:@"Error: %D", errorNumber]

message:[[NSString alloc] initWithFormat:@"This is an error"]



otherButtonTitles: nil];

[error show];

[error release];


In the YourView.m file you are calling this new method from:

#import "YourFunctions.h"

// As a test I attached a button to this action.

- (IBAction)showAnAlert {

int errorNumber = 22;

YourFunctions *yourFunctions = [[YourFunctions alloc] init];

[yourFunctions showAlert:errorNumber];


Tuesday, March 23, 2010

iPhone Sequential Execution

Something that I've found a little odd in iPhone code is that things don't always execute sequentially. Actually, I'm sure they do, but because they all seem to load at once they don't seem like it. I found a great method of something executing right away. By encapsulating a method in the following function, things before it run, and then this executes and runs. This is especially useful to be so that I can have an activity indicator (spinning ball) run while this happens in the background.

// Start up our activity indicator while we collect the data

[self startActivityIndicator];

// Run the loadData method right now

[self performSelector:@selector(loadDataInBackground) withObject:nil afterDelay:0.0];

I can then end the Activity Indicator when I get to the end of the "loadDataInBackground" method. If we just call both of the methods, the ActivityIndicator doesn't display until just as the loadData method ends.