# bcrypt project

**URL:** <https://forum.xojo.com/t/bcrypt-project/13839>\
**Category:** General\
**Created:** [January 23, 2014, 3:52pm UTC](https://forum.xojo.com/t/bcrypt-project/13839 "2014-01-23T15:52:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 23, 2014, 3:52pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/1 "2014-01-23T15:52:26Z")

</div>

I’d like to port bcrypt, found [here](http://ftp3.usa.openbsd.org/pub/OpenBSD/src/lib/libc/crypt/bcrypt.c), to Xojo, but I’m afraid my C is just not up to the task, so I thought I’d throw a few specific questions out there to help move it along. I’ll make the final product available when finished.

If there is enough interest, I’ll even throw the project up onto GitHub.

The first question comes from this code:

```auto
static void
encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
{
	u_int8_t *bp = buffer;
	u_int8_t *p = data;
	u_int8_t c1, c2;
	while (p < data + len) {
		c1 = *p++;
		*bp++ = Base64Code[(c1 >> 2)];
		c1 = (c1 & 0x03) << 4;
		if (p >= data + len) {
			*bp++ = Base64Code[c1];
			break;
		}
		c2 = *p++;
		c1 |= (c2 >> 4) & 0x0f;
		*bp++ = Base64Code[c1];
		c1 = (c2 & 0x0f) << 2;
		if (p >= data + len) {
			*bp++ = Base64Code[c1];
			break;
		}
		c2 = *p++;
		c1 |= (c2 >> 6) & 0x03;
		*bp++ = Base64Code[c1];
		*bp++ = Base64Code[c2 & 0x3f];
	}
	*bp = '\\0';
}
```

What is an `u_int8_t`? I get that it’s an UInt8, but what’s the “\_t” about? The answer to this will help me understand the parameters and how they are being used.

And does this code ultimately do the same as Xojo’s `EncodeBase64`?

---

<div class="post-metadata">

**Author:** ![Joshua\_Woods](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@Joshua\_Woods](https://forum.xojo.com/u/Joshua_Woods)\
**Post date:** [January 23, 2014, 4:06pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/2 "2014-01-23T16:06:32Z")

</div>

I’m no expert but I’m fairly sure the \_t is simply a naming convention to show that it’s been typedef’d. Fairly common to see in C.

Evidence:  
[http://lxr.free-electrons.com/source/include/uapi/linux/coda.h#L123](http://lxr.free-electrons.com/source/include/uapi/linux/coda.h#L123)  
[http://stackoverflow.com/questions/1391447/what-does-the-postfix-t-stand-for-in-c](http://stackoverflow.com/questions/1391447/what-does-the-postfix-t-stand-for-in-c)

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 23, 2014, 4:25pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/3 "2014-01-23T16:25:47Z")

</div>

Got it. So the C code:

```auto
static void
encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
```

Would translate to Xojo as:

```auto
Function encode_base64 (ByRef buffer As UInt8, ByRef data As UInt8, thelen As UInt16)
```

Yes?

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [January 23, 2014, 4:52pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/4 "2014-01-23T16:52:28Z")

</div>

encode\_base64(buffer As MemoryBlock, data As MemoryBlock)

thelen -\> data.size

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 23, 2014, 4:59pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/5 "2014-01-23T16:59:40Z")

</div>

Except it looks like this function is expecting a single value, and will use it to do calculations, so `u_int8_t *buffer` is a pointer to a single byte, isn’t it?

I feel like I’m missing something in how C treats these variables.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [January 23, 2014, 5:10pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/6 "2014-01-23T17:10:13Z")

</div>

why would you want to port a base64 encode function to Xojo where already one exists?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 23, 2014, 5:14pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/7 "2014-01-23T17:14:57Z")

</div>

I’m using this as an example to learn. The answers here will help me understand other parts of the code.

And you missed the last line of my original question.

> [@60954:@Kem Tekinay](#):
>
> And does this code ultimately do the same as Xojo’s EncodeBase64?

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [January 23, 2014, 6:34pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/8 "2014-01-23T18:34:08Z")

</div>

[quote=60968:@Kem Tekinay]Except it looks like this function is expecting a single value, and will use it to do calculations, so `u_int8_t *buffer` is a pointer to a single byte, isn’t it?  
[/quote]  
more like the starting address of a sequence of bytes

the reason I say this is

` u_int8_t *bp = buffer;
   u_int8_t *p = data;
`  
and subsequently all the ptr math

`	c1 = *p++;
	*bp++ = Base64Code[(c1 >> 2)];`  
etc  
which steps through the byes pointed at by those two ptrs

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [January 23, 2014, 6:42pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/9 "2014-01-23T18:42:28Z")

</div>

Hi Kem,

u\_int8\_t \*buffer  
means “a pointer to” and not “a pointer to a single value” so it is something “like” a memory block but you don’t know how big it is the memory area.  
u\_int16\_t len reports how big it is the memory area (up to 65536 bytes in this case)

C language syntax…

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 23, 2014, 6:51pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/10 "2014-01-23T18:51:40Z")

</div>

Thank you both, that’s clearer, and a reminder of why I so love that Xojo doesn’t make me do pointer math. 🙂

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [January 23, 2014, 6:58pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/11 "2014-01-23T18:58:50Z")

</div>

Oops…

up to 65535 bytes… (while (p \< data + len))

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [January 24, 2014, 6:26pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/12 "2014-01-24T18:26:26Z")

</div>

Sorry I’m late to the party.

Could bcrypt be written as part of the crypto class like BER, DER, MD5, etc? I am asking for feasibility here not asking Kem to do it.

I think having bcrypt, blowfish, twofish, etc added to the Xojo crypto class would be a win for Xojo. The more crypto options the better.

Kem good luck with the port to xojo.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 24, 2014, 6:28pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/13 "2014-01-24T18:28:44Z")

</div>

Thanks. At this point, I’m still renewing my relationship with C. Like an almost-ex, I am being reminded why I didn’t want to get involved in the first place…

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [January 24, 2014, 6:31pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/14 "2014-01-24T18:31:28Z")

</div>

> [@61191:@Kem Tekinay](#):
>
> Thanks. At this point, I’m still renewing my relationship with C. Like an almost-ex, I am being reminded why I didn’t want to get involved in the first place…

Hahahahahahaha

C is my Ex. The crazy starker Ex that pops up when you least expect or want it to. And it is an Ex for a reason.

Good news is you are writing Xojo and just having to read/understand c. Which at times can’t be easy.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 24, 2014, 6:35pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/15 "2014-01-24T18:35:02Z")

</div>

It’s the pointer math I’m finding hard to decipher. \*p++ (or something) means something different than p++. Very annoying.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [January 24, 2014, 7:32pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/16 "2014-01-24T19:32:12Z")

</div>

[quote=60963:@Kem Tekinay]Got it. So the C code:

```auto
static void
encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
```

Would translate to Xojo as:

```auto
Function encode_base64 (ByRef buffer As UInt8, ByRef data As UInt8, thelen As UInt16)
```

[/quote]

Probably not - I think that the \*buffer and \*data should be considered “Pointers to an array of values” rather than “Pointers to one single value” so I’d write it as follows:

```auto
Function encode_base64 (buffer As MemoryBlock, data As MemoryBlock, thelen As UInt16)
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 24, 2014, 7:49pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/17 "2014-01-24T19:49:17Z")

</div>

Right. As Maurizio pointed out, theLen could probably be avoided too as it should be the same as data.Size.

---

<div class="post-metadata">

**Author:** ![Joshua\_Woods](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@Joshua\_Woods](https://forum.xojo.com/u/Joshua_Woods)\
**Post date:** [January 26, 2014, 2:53pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/18 "2014-01-26T14:53:52Z")

</div>

> [@61194:@Kem Tekinay](#):
>
> It’s the pointer math I’m finding hard to decipher. \*p++ (or something) means something different than p++. Very annoying.

The difference between \*p++ and p++ is this:

p refers to the pointer itself.  
\*p points at the pointer’s value.

p++ is to increment the pointer address (moving it to the next part of the array it points to, for example).  
\*p++ increments the value that the pointer points to.

I wrote this example to hopefully clarify further:

[code]// make an integer array with two elements  
int a[2];  
a[0] = 0;  
a[1] = 0;

// make a pointer  
int \*p;  
// point the pointer to the array’s first element  
p = &a[0];

// increase the value of a[0] by 1  
\*p++;

// print this value  
printf("%d  
", \*p); // 1 is printed

// make p point to a[1]  
p++;

// print this value  
printf("%d  
", \*p); // 0 is printed[/code]

With this in mind you can always print the pointer’s address by referring to p. Then when you want to print the pointer’s value you refer to \*p.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 26, 2014, 5:38pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/19 "2014-01-26T17:38:05Z")

</div>

Thanks Joshua, that was very helpful. Believe it or not, I knew this at one time, but that’s how far removed I’ve become from trying to learn C. 🙂

So, with this in mind, I think I’ve translated this code faithfully. (I realize this is not the optimal way to do it in Xojo given its tools, but my goal here is get the closest translation I can get. I’ll optimize later.)

Again, the C code:

```auto
static void
encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
{
	u_int8_t *bp = buffer;
	u_int8_t *p = data;
	u_int8_t c1, c2;
	while (p < data + len) {
		c1 = *p++;
		*bp++ = Base64Code[(c1 >> 2)];
		c1 = (c1 & 0x03) << 4;
		if (p >= data + len) {
			*bp++ = Base64Code[c1];
			break;
		}
		c2 = *p++;
		c1 |= (c2 >> 4) & 0x0f;
		*bp++ = Base64Code[c1];
		c1 = (c2 & 0x0f) << 2;
		if (p >= data + len) {
			*bp++ = Base64Code[c1];
			break;
		}
		c2 = *p++;
		c1 |= (c2 >> 6) & 0x03;
		*bp++ = Base64Code[c1];
		*bp++ = Base64Code[c2 & 0x3f];
	}
	*bp = '\\0';
}
```

My translation. Does it appear that I’ve gotten this right?

```auto
Private Sub encode_base64(buffer As MemoryBlock, data As MemoryBlock)
  dim lastByteIndex as integer = data.Size - 1
  dim bp, p as integer
  
  dim c1, c2 as byte
  while ( p <= lastByteIndex )
    c1 = data.Byte( p )
    p = p + 1
    
    buffer.Byte( bp ) = Base64Code( Bitwise.ShiftRight( c1, 2 ) )
    bp = bp + 1
    
    c1 = Bitwise.ShiftLeft( c1 and &h03, 4 )
    if p > lastByteIndex then
      buffer.Byte( bp ) = Base64Code( c1 )
      exit while
    end if
    
    c2 = data.Byte( p )
    p = p + 1
    
    c1 = c1 or ( Bitwise.ShiftRight( c2, 4 ) and &h0F )
    buffer.Byte( bp ) = Base64Code( c1 )
    bp = bp + 1
    
    c1 = Bitwise.ShiftLeft( c2 and &h0F, 2 )
    if p > lastByteIndex then
      buffer.Byte( bp ) = Base64Code( c1 )
      bp = bp + 1
      exit while
    end if
    
    c2 = data.Byte( p )
    p = p + 1
    
    c1 = c1 or ( Bitwise.ShiftLeft( c2, 6 ) and &h03 )
    buffer.Byte( bp ) = Base64Code( c1 )
    bp = bp + 1
    buffer.Byte( bp ) = Base64Code( c2 and &h3F )
    bp = bp + 1
  wend
  
  buffer.Byte( bp ) = 0  
End Sub
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 26, 2014, 6:48pm UTC](https://forum.xojo.com/t/bcrypt-project/13839/20 "2014-01-26T18:48:45Z")

</div>

I should note that this code works without error, but the result differs completely from what I get from `EncodeBase64`.

[Next page](https://forum.xojo.com/t/bcrypt-project/13839.md?page=2)
