Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hendrikvincent 0:05ccbd4f84f1 1 /* Copyright (C) 2007 MySQL AB & Michael Widenius
hendrikvincent 0:05ccbd4f84f1 2
hendrikvincent 0:05ccbd4f84f1 3 This program is free software; you can redistribute it and/or modify
hendrikvincent 0:05ccbd4f84f1 4 it under the terms of the GNU General Public License as published by
hendrikvincent 0:05ccbd4f84f1 5 the Free Software Foundation; version 2 of the License.
hendrikvincent 0:05ccbd4f84f1 6
hendrikvincent 0:05ccbd4f84f1 7 This program is distributed in the hope that it will be useful,
hendrikvincent 0:05ccbd4f84f1 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
hendrikvincent 0:05ccbd4f84f1 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hendrikvincent 0:05ccbd4f84f1 10 GNU General Public License for more details.
hendrikvincent 0:05ccbd4f84f1 11
hendrikvincent 0:05ccbd4f84f1 12 You should have received a copy of the GNU General Public License
hendrikvincent 0:05ccbd4f84f1 13 along with this program; if not, write to the Free Software
hendrikvincent 0:05ccbd4f84f1 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
hendrikvincent 0:05ccbd4f84f1 15
hendrikvincent 0:05ccbd4f84f1 16 #define SCRAMBLE_LENGTH_323 8
hendrikvincent 0:05ccbd4f84f1 17
hendrikvincent 0:05ccbd4f84f1 18 #include <string.h>
hendrikvincent 0:05ccbd4f84f1 19 #include <math.h>
hendrikvincent 0:05ccbd4f84f1 20
hendrikvincent 0:05ccbd4f84f1 21 typedef unsigned int uint;
hendrikvincent 0:05ccbd4f84f1 22 typedef unsigned long ulong;
hendrikvincent 0:05ccbd4f84f1 23 typedef unsigned char uchar;
hendrikvincent 0:05ccbd4f84f1 24
hendrikvincent 0:05ccbd4f84f1 25 struct my_rnd_struct {
hendrikvincent 0:05ccbd4f84f1 26 unsigned long seed1,seed2,max_value;
hendrikvincent 0:05ccbd4f84f1 27 double max_value_dbl;
hendrikvincent 0:05ccbd4f84f1 28 };
hendrikvincent 0:05ccbd4f84f1 29
hendrikvincent 0:05ccbd4f84f1 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
hendrikvincent 0:05ccbd4f84f1 31 static double my_rnd(struct my_rnd_struct *rand_st);
hendrikvincent 0:05ccbd4f84f1 32 static void hash_password(ulong *result, const char *password, uint password_len);
hendrikvincent 0:05ccbd4f84f1 33
hendrikvincent 0:05ccbd4f84f1 34 /*
hendrikvincent 0:05ccbd4f84f1 35 Initialize random generator
hendrikvincent 0:05ccbd4f84f1 36
hendrikvincent 0:05ccbd4f84f1 37 NOTES
hendrikvincent 0:05ccbd4f84f1 38 MySQL's password checks depends on this, so don't do any changes
hendrikvincent 0:05ccbd4f84f1 39 that changes the random numbers that are generated!
hendrikvincent 0:05ccbd4f84f1 40 */
hendrikvincent 0:05ccbd4f84f1 41
hendrikvincent 0:05ccbd4f84f1 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
hendrikvincent 0:05ccbd4f84f1 43 {
hendrikvincent 0:05ccbd4f84f1 44 rand_st->max_value= 0x3FFFFFFFL;
hendrikvincent 0:05ccbd4f84f1 45 rand_st->max_value_dbl=(double) rand_st->max_value;
hendrikvincent 0:05ccbd4f84f1 46 rand_st->seed1=seed1%rand_st->max_value ;
hendrikvincent 0:05ccbd4f84f1 47 rand_st->seed2=seed2%rand_st->max_value;
hendrikvincent 0:05ccbd4f84f1 48 }
hendrikvincent 0:05ccbd4f84f1 49
hendrikvincent 0:05ccbd4f84f1 50 /*
hendrikvincent 0:05ccbd4f84f1 51 Generate random number.
hendrikvincent 0:05ccbd4f84f1 52
hendrikvincent 0:05ccbd4f84f1 53 SYNOPSIS
hendrikvincent 0:05ccbd4f84f1 54 my_rnd()
hendrikvincent 0:05ccbd4f84f1 55 rand_st INOUT Structure used for number generation
hendrikvincent 0:05ccbd4f84f1 56
hendrikvincent 0:05ccbd4f84f1 57 RETURN VALUE
hendrikvincent 0:05ccbd4f84f1 58 generated pseudo random number
hendrikvincent 0:05ccbd4f84f1 59 */
hendrikvincent 0:05ccbd4f84f1 60
hendrikvincent 0:05ccbd4f84f1 61 static double my_rnd(struct my_rnd_struct *rand_st)
hendrikvincent 0:05ccbd4f84f1 62 {
hendrikvincent 0:05ccbd4f84f1 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
hendrikvincent 0:05ccbd4f84f1 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
hendrikvincent 0:05ccbd4f84f1 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
hendrikvincent 0:05ccbd4f84f1 66 }
hendrikvincent 0:05ccbd4f84f1 67
hendrikvincent 0:05ccbd4f84f1 68 /*
hendrikvincent 0:05ccbd4f84f1 69 Generate binary hash from raw text string
hendrikvincent 0:05ccbd4f84f1 70 Used for Pre-4.1 password handling
hendrikvincent 0:05ccbd4f84f1 71 SYNOPSIS
hendrikvincent 0:05ccbd4f84f1 72 hash_password()
hendrikvincent 0:05ccbd4f84f1 73 result OUT store hash in this location
hendrikvincent 0:05ccbd4f84f1 74 password IN plain text password to build hash
hendrikvincent 0:05ccbd4f84f1 75 password_len IN password length (password may be not null-terminated)
hendrikvincent 0:05ccbd4f84f1 76 */
hendrikvincent 0:05ccbd4f84f1 77
hendrikvincent 0:05ccbd4f84f1 78 static void hash_password(ulong *result, const char *password, uint password_len)
hendrikvincent 0:05ccbd4f84f1 79 {
hendrikvincent 0:05ccbd4f84f1 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
hendrikvincent 0:05ccbd4f84f1 81 ulong tmp;
hendrikvincent 0:05ccbd4f84f1 82 const char *password_end= password + password_len;
hendrikvincent 0:05ccbd4f84f1 83 for (; password < password_end; password++)
hendrikvincent 0:05ccbd4f84f1 84 {
hendrikvincent 0:05ccbd4f84f1 85 if (*password == ' ' || *password == '\t')
hendrikvincent 0:05ccbd4f84f1 86 continue; /* skip space in password */
hendrikvincent 0:05ccbd4f84f1 87 tmp= (ulong) (uchar) *password;
hendrikvincent 0:05ccbd4f84f1 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
hendrikvincent 0:05ccbd4f84f1 89 nr2+=(nr2 << 8) ^ nr;
hendrikvincent 0:05ccbd4f84f1 90 add+=tmp;
hendrikvincent 0:05ccbd4f84f1 91 }
hendrikvincent 0:05ccbd4f84f1 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
hendrikvincent 0:05ccbd4f84f1 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
hendrikvincent 0:05ccbd4f84f1 94 }
hendrikvincent 0:05ccbd4f84f1 95
hendrikvincent 0:05ccbd4f84f1 96
hendrikvincent 0:05ccbd4f84f1 97
hendrikvincent 0:05ccbd4f84f1 98 /*
hendrikvincent 0:05ccbd4f84f1 99 Scramble string with password.
hendrikvincent 0:05ccbd4f84f1 100 Used in pre 4.1 authentication phase.
hendrikvincent 0:05ccbd4f84f1 101 SYNOPSIS
hendrikvincent 0:05ccbd4f84f1 102 scramble_323()
hendrikvincent 0:05ccbd4f84f1 103 to OUT Store scrambled message here. Buffer must be at least
hendrikvincent 0:05ccbd4f84f1 104 SCRAMBLE_LENGTH_323+1 bytes long
hendrikvincent 0:05ccbd4f84f1 105 message IN Message to scramble. Message must be at least
hendrikvincent 0:05ccbd4f84f1 106 SRAMBLE_LENGTH_323 bytes long.
hendrikvincent 0:05ccbd4f84f1 107 password IN Password to use while scrambling
hendrikvincent 0:05ccbd4f84f1 108 */
hendrikvincent 0:05ccbd4f84f1 109
hendrikvincent 0:05ccbd4f84f1 110 void scramble_323(char *to, const char *message, const char *password)
hendrikvincent 0:05ccbd4f84f1 111 {
hendrikvincent 0:05ccbd4f84f1 112 struct my_rnd_struct rand_st;
hendrikvincent 0:05ccbd4f84f1 113 ulong hash_pass[2], hash_message[2];
hendrikvincent 0:05ccbd4f84f1 114
hendrikvincent 0:05ccbd4f84f1 115 if (password && password[0])
hendrikvincent 0:05ccbd4f84f1 116 {
hendrikvincent 0:05ccbd4f84f1 117 char extra, *to_start=to;
hendrikvincent 0:05ccbd4f84f1 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
hendrikvincent 0:05ccbd4f84f1 119 hash_password(hash_pass,password, (uint) strlen(password));
hendrikvincent 0:05ccbd4f84f1 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
hendrikvincent 0:05ccbd4f84f1 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
hendrikvincent 0:05ccbd4f84f1 122 hash_pass[1] ^ hash_message[1]);
hendrikvincent 0:05ccbd4f84f1 123 for (; message < message_end; message++)
hendrikvincent 0:05ccbd4f84f1 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
hendrikvincent 0:05ccbd4f84f1 125 extra=(char) (floor(my_rnd(&rand_st)*31));
hendrikvincent 0:05ccbd4f84f1 126 while (to_start != to)
hendrikvincent 0:05ccbd4f84f1 127 *(to_start++)^=extra;
hendrikvincent 0:05ccbd4f84f1 128 }
hendrikvincent 0:05ccbd4f84f1 129 *to= 0;
hendrikvincent 0:05ccbd4f84f1 130 }
hendrikvincent 0:05ccbd4f84f1 131